【问题标题】:android notification always relaunches activityandroid通知总是重新启动活动
【发布时间】:2013-04-11 05:55:43
【问题描述】:

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

我正在尝试通过单击通知功能来实现梦寐以求的简历,但我遇到了一些困难。首先,详细信息(代码如下):

  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 对象在下拉通知中创建一个栏。

我想要达到的行为:

  1. 启动带有通知栏条目的应用程序(例如邮件)
  2. 启动 MyApp(这也将启动 MyAppService)
  3. 下拉通知栏并选择MyAppService(是的,MyApp目前在屏幕上
  4. 通知栏应该只是卷起来而不做任何事情

我注意到 MyApp 将最小化,而 MyApp 的新实例将最大化(类似于启动应用程序时的过渡)。当我在顶部有邮件应用程序(邮件应用程序最小化和 MyApp 最大化)时,我希望在 MyAppService 上选择这种行为。

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

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 已经在最上面,我只想让通知下拉菜单向上滚动。

提前致谢!

【问题讨论】:

  • 当它是最顶层的活动时,为您自己的应用程序提供通知并不是一个好主意;通知用于在用户面前有其他内容时通知用户与您的应用相关的内容。我建议您在其活动处于顶部时取消应用的所有通知。
  • 哦,另外,如果你也在使用startForeground(),你不需要自己打电话给notify();后一个调用将负责发布通知。
  • 嘿@dsandler,通知实际上是由MyAppService.java维护的,这是MyApp启动时启动的服务。
  • Continuing... 我一直在阅读很多“我的 android 应用程序在单击通知时无法恢复”类型的帖子,它们对我不起作用。我想启动一个服务(MyAppService),它将运行并执行计划任务,MainActivity.java 将是最终用户可以启动以添加/编辑/删除计划任务的 GUI。
  • 好的,那么在这种情况下,您可能希望您的服务不是前台服务;这样您就可以在活动显示和隐藏时取消并重新发布通知。

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


【解决方案1】:
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 标志,我猜你可以实现这一点。如果它已经在运行,它将不会创建您的活动。

【讨论】:

    【解决方案2】:

    在我的情况下,Shashika 的答案有效,但方式很奇怪。我不得不重新启动电话! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多