Android通知栏点击无法启动Activity的问题

该博客已经停止更新,新博客点击此处:DevWiki的博客

最近在使用通知栏通知用户,用户点击通知栏后跳转到相应的界面.但是发现一个问题,在某些机型比如三星note2,OPPO find7上无法启动.但是在其他手机上正常.

通知栏的代码如下:

public static void showStartActivityNotification(Context context, Class claxx){
    NotificationManager manager = (NotificationManager) context.getApplicationContext().
            getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle("ContentTiel");
    builder.setContentText("ContentText");
    builder.setContentInfo("ContentInfo");
    builder.setAutoCancel(true);

    Intent intent = new Intent(context, claxx);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 
        PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setDefaults(Notification.DEFAULT_ALL);
    builder.setTicker("Ticker");
    manager.notify(new Random().nextInt(), builder.build());
}

要启动的Activity的配置如下:

<activity android:name="com.asia.commonutil.SecondActivity">activity>

经过测试发现在三星Note2显示通知时会打印以下日志:

??-?? ??:??:??.???: INFO/(): [SANotificationServiceNotificationListener] onNotificationPosted(com.asia.commonutil), UserID : 0, Listener : com.samsung.accessory.goproviders.sanotificationservice.SANotificationServiceSystemNotificationListener@42601a00
??-?? ??:??:??.???: INFO/(): Permission Denial: starting Intent { cmp=com.asia.commonutil/.SecondActivity bnds=[0,680][720,808] (has extras) } from null (pid=-1, uid=10343) not exported from uid 10357
??-?? ??:??:??.???: INFO/(): java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.asia.commonutil/.SecondActivity bnds=[0,680][720,808] (has extras) } from null (pid=-1, uid=10343) not exported from uid 10357
??-?? ??:??:??.???: INFO/(): [SANotificationServiceNotificationListener] onNotificationRemoved(com.asia.commonutil)

在里面我发现这样一句:

java.lang.SecurityException: Permission Denial:

意思是没有权限.怎么可能没有权限?突然又发现这样一句:

[SANotificationServiceNotificationListener] onNotificationPosted(com.asia.commonutil), UserID : 0, Listener : com.samsung.accessory.goproviders.sanotificationservice.SANotificationServiceSystemNotificationListener@42601a00

原来是系统要来启动SecondActivity这个界面,但是这个界面没有配置被外界启动

所以修改Activity配置如下:

<activity android:name="com.asia.commonutil.SecondActivity"
    android:exported="true">
activity>

然后就可以启动了.如果你也遇到此问题,也可以试试.如若转载,请附上原文地址,谢谢!

你可能感兴趣的:(Android,开发技巧)