首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通知无法转到当前活动

通知无法转到当前活动
EN

Stack Overflow用户
提问于 2014-04-02 18:20:46
回答 2查看 234关注 0票数 1

在我的通知中,我希望在单击待定意向时启动当前活动。在谷歌中,找到了很多方法,我也尝试了很多次,但都不起作用。这是我的代码,

代码语言:javascript
运行
复制
          public void onStart(Intent intent, int startId)
   {
     super.onStart(intent, startId);

     String startTime = intent.getStringExtra("Strar_time");

     NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();         // notification time
        Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        notification.vibrate = new long[]{100, 200, 100, 500};
        notification.defaults = Notification.DEFAULT_ALL;

        Intent notificationIntent = new Intent(this, Goo.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/);

        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent);
        notification.contentIntent=contentIntent;
        nm.notify(NOTIF_ID, notification);
        NOTIF_ID++;
        Log.i("NotiID",NOTIF_ID+"");
    }
EN

回答 2

Stack Overflow用户

发布于 2014-04-02 18:37:54

尝试下面给出的函数,

代码语言:javascript
运行
复制
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.launch_icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = null;

    notificationIntent = new Intent(context, Main.class);

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}
票数 0
EN

Stack Overflow用户

发布于 2014-04-02 18:43:35

到目前为止,我发现唯一可行的方法是创建一个虚拟活动,启动它,然后在onRusume()中完成它。然而,该技术仅将应用程序置于前端,且在应用程序关闭时不启动应用程序。然后,我使用以下变通方法,如果应用程序在后台处于活动状态,则将其置于前端,否则启动它。

清单:

代码语言:javascript
运行
复制
<application>
.
.
.
 <activity
 android:name=".DummyActivity"
 />

.
.
</application>

代码中的

代码语言:javascript
运行
复制
public class DummyActivity{

public static boolean isAppAlive;

    @Override
    protected void onResume() {
      // start application if it is not alive
       if(!isAppAlive){
         PackageManager  pmi = getPackageManager();
         Intent intent = pmi.getLaunchIntentForPackage(Your applications package name);                    
          if (intent != null){
              startActivity(intent);
           }

      }
    finish();
    }

在主活动的onCreateMethod中。

代码语言:javascript
运行
复制
@Override
public void onCreate(Bundle savedInstanceState) {
  DummyActivity.isAppAlive = true;
}

很明显,当您的应用程序完全关闭时,isAppAlive将恢复为默认值false。因此应用程序将被启动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22808358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档