android通知总是重新启动活动

时间:2013-04-11 05:55:43

标签: android notifications android-4.2-jelly-bean

这里的第一次海报,请原谅我冗长的帖子 - 我想提供所有必要的信息。

我试图通过点击通知功能来实现令人垂涎的简历,但我遇到了一些困难。首先,详细信息(下面的代码):

  1. 项目属性

    Android Developer Tools Build: v21.1.0-569685
    Project Build Target : 4.2.2
    Platofrm : 4.2.2
    API Level : 17
    SDK Target : 17
    SDK min : 14
    
    Android Device Chooser : Android Device : Nexus 4 
    
  2. MyApp 通过startService启动 MyAppService
  3. MyAppService 的onStartCommand函数使用NotificationCompat.Builder对象在下拉通知中创建一个条。
  4. 我想要实现的行为:

    1. 启动带有通知栏条目的应用程序(例如邮件)
    2. 启动MyApp(这也将启动MyAppService)
    3. 下拉通知栏并选择MyAppService(是的,MyApp当前在屏幕上
    4. 通知栏应该只是卷起来做任何事情
    5. 我注意到MyApp将最小化,MyApp的新实例将最大化(类似于应用程序启动时的过渡)。当我在邮件应用程序位于顶部时选择MyAppService时,我期望这种行为(邮件应用程序最小化,MyApp最大化)。

      有很多代码片段和建议,但我似乎无法让它工作;所以这是我的文件的内容:

      的AndroidManifest.xml

      <application
          android:allowBackup="true"
          android:icon="@drawable/eyeview"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
      
          <activity
              android:name="com.example.MyApp.MainActivity"
              android:label="@string/app_name"
              android:launchMode="singleTop"
              >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
      
          <service
              android:name=".service.MyAppService"
              android:label="MyAppService">
          </service>
      </application>
      

      MyApp的/ MainActivity.java

      public static Intent serviceIntent;
      public static Messenger clientMessenger;
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstance);
      
          if (getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
          {
              finish();
              return;
          }
      
          setContentView(R.layout.activity_main);
      
          ...
          [stuff specific to app, like setting up Fragments]
          ...
      
          MainActivity.serviceIntent = new Intent(this, MyAppService.class);
          MainActivity.serviceIntent.putExtra("clientMessenger", clientMessenger);
          startService(MainActivity.serviceIntent);
      }
      

      MyAppService.java

      @Override
      private int onStartCommand(Intent intent, int flags, int startId)
      {
          NotificationCompat.Builder ncB = new NotificationCompat.Builder(getApplicationContext())
              .setSmallIcon(R.drawable.icon_app)
              .setContentTitle("MyAppService")
              .setSmallIcon(R.drawable.app);
      
          ncB.setContentText("Click to start MyApp");
      
          Intent nIntent = new Intent(getApplicationContext(), MainActivity.class);
          nIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
          nIntent.setAction(Intent.ACTION_MAIN);
      
          TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
          stackBuilder.addNextIntent(nIntent);
          PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
      
          notificationBuilder.setContentIntent(resultPendingIntent);
          NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
          int nId = 1;
          Notification notification = ncB.build();
          notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
      
          notificationManager.notify(nId, notification); 
      
          sendMessengerToClient(intent);
      
          startForeground(nId, ncB.build());
      
          return Service.START_NOT_STICKY;
      }
      

      我希望我收集了所有足够的信息 - 我一直在调整我从SO获得的代码,看看是否可行 - 在每种情况下,最顶层的应用程序最小化到黑屏,MyApp将最大化和MainActivity调用.java的onCreate函数。

      我只是希望通知下拉只是向上滚动,如果MyApp已经是最顶层的。

      提前致谢!

2 个答案:

答案 0 :(得分:4)

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);

通过设置PendingIntent.FLAG_UPDATE_CURRENTIntent.FLAG_ACTIVITY_SINGLE_TOP标记,您可以实现此目标。如果已经在运行,它将不会创建您的活动。

答案 1 :(得分:2)

就我而言,Shashika的答案是有效的,但却是一种奇怪的方式。我不得不重新启动手机! :)