从服务通知

时间:2015-05-19 10:08:23

标签: android android-activity android-notifications

首先让我说,我知道这是一个与其他人已经得到回答的非常相似的问题,但他们并没有帮助我解决我的问题。

问题Resume application and stack from notification与我的问题完全相同,但选定的答案对我不起作用,我不太明白为什么会这样?

简单地说,我希望能够从通知点击中恢复我的应用程序,无论打开什么活动,我希望它返回上一个打开的活动。所以,如果我在活动F,然后按主页按钮几分钟后我按下通知..我想返回活动F.我不能回到MainActivity,因为用户有时被迫留在某种活动。

此答案 - https://stackoverflow.com/a/5502950/4662037如下所示:

final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

似乎是正确的,因为很多人已经接受了它,但在这种情况下究竟是什么“YourActivity”?我不想将其设置为特定的活动!

编辑:更新了通知代码

private void setForegroundService(){
    CharSequence text = getString(R.string.app_name);

    PackageManager pm = getPackageManager();

    Intent notificationIntent = pm.getLaunchIntentForPackage("com.example.xxxx.xxxxx");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
            notificationIntent, 0);

    // Create the notification
    Notification notification = new NotificationCompat.Builder(this)
            .setContentText(text)
            .setSmallIcon(R.drawable.nurse_call_app_icon)
            .setContentTitle(text)
            .setContentIntent(contentIntent).build();

    notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

    //  Start the actual service
    startForeground(Integer.MAX_VALUE, notification);
}

清单代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xxxxx.xxxxxx"
    android:versionCode="1"
    android:versionName="0.0.1" >

<uses-sdk android:minSdkVersion="14" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-feature android:name="android.hardware.Camera" />
<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="false" />

<application
    android:name=".xxxxxxx"
    android:allowBackup="true"
    android:icon="@drawable/xxxxxx"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <service android:name=".xxxxxxService" />

    <activity
        android:name=".activities.LoginActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".activities.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".activities.AlarmActivity"
        android:icon="@drawable/ic_action_alarms"
        android:label="@string/alarm_details"
        android:parentActivityName=".activities.MainActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity" />
    </activity>
    <activity
        android:name=".activities.SettingsActivity"
        android:icon="@drawable/ic_action_settings"
        android:label="@string/settings"
        android:parentActivityName=".activities.MainActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity" />
    </activity>
    <activity
        android:name=".activities.MessageListActivity"
        android:icon="@drawable/ic_action_email"
        android:label="@string/title_activity_text"
        android:launchMode="singleTask"
        android:parentActivityName=".activities.MainActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity" />
    </activity>
    <activity
        android:name=".activities.MessageActivity"
        android:icon="@drawable/ic_action_read"
        android:label="@string/message"
        android:parentActivityName=".activities.MessageListActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MessageListActivity" />
    </activity>
    <activity
        android:name=".activities.BreakActivity"
        android:icon="@drawable/ic_action_person"
        android:label="@string/title_activity_break"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".activities.ContactsActivity"
        android:icon="@drawable/ic_action_group"
        android:label="@string/title_activity_contacts"
        android:parentActivityName=".activities.MainActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity" />
    </activity>
    <activity
        android:name=".activities.CallActivity"
        android:icon="@drawable/ic_action_call"
        android:label="@string/title_activity_call"
        android:screenOrientation="portrait" >
    </activity>

    <receiver
        android:name=".BootUpReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".PhoneCallReceiver"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

快速概述它的作用:

从LoginActivity开始,登录后会破坏它,然后MainActivity将成为一切的根源。您可以在堆栈之间上下导航,但不要低于MainActivity,除非您调用注销。

3 个答案:

答案 0 :(得分:4)

查看我的问题here

这就是你应该这样做的方式:

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);

Notification notification = new Notification(R.drawable.floating2, "Click to start launcher",System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(), "Start launcher" ,  "Click to start launcher", pendingIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(ID_NOTIFICATION,notification);

答案 1 :(得分:0)

YourActivity是根活动。这是您的清单中使用ACTION = MAIN和CATEGORY = DEFAULT定义的那个。

您还需要设置FLAG_ACTIVITY_NEW_TASK

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

这实际上并不启动任何Activity(如果您的应用已在运行)。它只是将包含您的应用程序的任务带到前台,状态与进入后台时的状态相同,这就是您想要的。

<强>更新

由于您似乎遇到此问题,请尝试以下替代建议:

PackageManager pm = getPackageManager();
// NOTE: replace com.example.xxx.xxx with the package name from your manifest!
Intent notificationIntent = pm.getLaunchIntentForPackage("com.example.xxx.xxx");
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                                   notificationIntent, 0);

答案 2 :(得分:0)

我通过在onResume的{​​{1}}方法中保存主要活动意图来解决此问题,如下所示:

BaseActivity

点击“ push”后,我得到了最高活动意图。

您需要做的就是利用两个flags来了解最近的热门活动 Intent currentActivityIntent = new Intent(this, getClass()); currentActivityIntent.addFlags(FLAG_ACTIVITY_NEW_TASK); currentActivityIntent.addFlags(FLAG_ACTIVITY_SINGLE_TOP); App.setCurrentActivityIntent(currentActivityIntent); FLAG_ACTIVITY_NEW_TASK