单击通知时启动活动

时间:2016-05-30 10:24:13

标签: android android-notifications

当我点击状态栏中的通知时,我想打开一个活动。我在StackOverflow上看到了这个问题,但这些答案都不适用于我。这是我的代码:

    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setProgress(100, 0, false);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
    notificationBuilder.setContentTitle(getString(R.string.notification_upload_title));

    //when this notification is clicked and the upload is running, open the upload fragment
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    // set intent so it does not start a new activity
    PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    notificationBuilder.setContentIntent(intent);

    this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build());

的Manifest.xml

<activity
        android:name="com.android.app.MainActivity_"
        android:label="@string/app_short_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

5 个答案:

答案 0 :(得分:2)

最后我意识到我必须写下这个:

Intent notificationIntent = new Intent(this, MainActivity_.class);

而不是

Intent notificationIntent = new Intent(this, MainActivity.class);

非常感谢您的所有答案!

答案 1 :(得分:1)

我不认为你需要所有这些标志和配置来打开PendingIntent中的活动。你当然不需要

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent.FLAG_ONE_SHOT

删除它们然后重试。

此外:是否符合预期的MainActivity的包名称( com.android.app.MainActivity )?

答案 2 :(得分:0)

尝试以下解决方案希望它适合您

Intent intent = new Intent(this,YourActivity....class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent blogIntent = PendingIntent.getActivity(this, INT CONSTANTS, intent,
                PendingIntent.FLAG_ONE_SHOT);

来自通知

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
.
.
.
notificationBuilder.setContentIntent(blogIntent);


Notification notification = notificationBuilder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(Constants.NOTIFICATION_NEW_BLOG, notification);

答案 3 :(得分:0)

您必须在清单

中添加自定义接收器
       <receiver
            android:name=".IntentReceiver"
            android:exported="false">
            <intent-filter>

                <!-- add notification action here  -->

            </intent-filter>
        </receiver>

在Intent接收器类中覆盖接收方法

public class IntentReceiver extends BroadcastReceiver{
@Override
    public void onReceive(Context context, Intent intent) {
        //call your activity here
    }
} 

答案 4 :(得分:0)

RelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutID);
layout.addView(myButton);