通知无法进行当前活动

时间:2014-04-02 10:20:46

标签: android android-intent notifications

在我的通知中,我想在点击待处理意图时启动当前活动。在谷歌,发现了很多方法,我尝试了很多次,它没有工作。这是我的代码,

          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+"");
    }

2 个答案:

答案 0 :(得分:0)

尝试下面给出的这个功能,

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);

}

答案 1 :(得分:0)

到目前为止,我发现只有一种工作方式是进行虚拟活动,启动它并在onRusume()中完成它。但是,这种技术只会将应用程序带到前面,并且在关闭时不会启动应用程序。 然后我使用以下解决方法将应用程序放在前面,如果它在后台运行,否则启动它。

<强>清单:

<application>
.
.
.
 <activity
 android:name=".DummyActivity"
 />

.
.
</application>

代码:

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。

@Override
public void onCreate(Bundle savedInstanceState) {
  DummyActivity.isAppAlive = true;
}

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