2014-04-02 58 views
1

在我的通知中,我想在点击待处理意图时启动当前活动。在谷歌,找到很多方法,我尝试了很多次,但没有奏效。这里是我的代码,通知无法执行当前活动

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

回答

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

} 
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。因此应用程序将开始。

相关问题