Android通知可能会打开该应用两次(甚至更多次)

时间:2018-10-04 17:43:21

标签: android notifications instance

我的应用使用以下代码设置通知:

private void defineAndLaunchNotification(String apppack,String title, String ContentText)
    {
Context context = getApplicationContext();
PackageManager pm = context.getPackageManager();
Intent LaunchIntent = null;
String name=null;

try {
    if (pm != null)
    {

        ApplicationInfo app = context.getPackageManager().getApplicationInfo(apppack, 0);
        name = (String) pm.getApplicationLabel(app);
        LaunchIntent = pm.getLaunchIntentForPackage(apppack);
    }
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();

}

Intent intent = LaunchIntent; 

if (ContentText.equals("filesystemfullnotification"))
{
    intent.putExtra("start", "fullsystem");
}
else
{
    intent.putExtra("start","incorrecttime");
}

PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);


NotificationCompat.Builder builder=null;
NotificationChannel notificationChannel=null;
int NOTIFICATION_ID = 12345;
if (Build.VERSION.SDK_INT<26) {
    builder =
            new NotificationCompat.Builder(getBaseContext())
                    .setSmallIcon(R.drawable.notification_icon)
                    .setAutoCancel(true)

                    .setContentTitle("title
)


 .setContentText("Content Text");


}
else
{
    int importance=NotificationManager.IMPORTANCE_HIGH;
    notificationChannel=new NotificationChannel("mychannel", "channel", importance);

    builder =
            new NotificationCompat.Builder(getBaseContext(),"mychannel")
                    .setSmallIcon(R.drawable.notification_icon)
                    .setAutoCancel(true)
                    //.setStyle(new NotificationCompat.BigTextStyle().bigText(StaticMethods.giveStringAccordingtoLanguage(title,language)))
                    .setContentTitle(StaticMethods.giveStringAccordingtoLanguage(title, language))
                    .setContentText(StaticMethods.giveStringAccordingtoLanguage(ContentText, language));



}

builder.addAction(R.drawable.notification_icon, "OK", pIntent);


Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setVibrate(new long[]{0, 1000, 1000, 1000, 1000});


builder.setContentIntent(pIntent);


NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT>=26) {
    nManager.createNotificationChannel(notificationChannel);
}

nManager.notify( (int) ((new Date().getTime() +Math.round(Math.random()*5000) / 1000L) % Integer.MAX_VALUE), builder.build());
}

该代码在每次调用时都会成功显示一条通知,但是问题在于,如果敲击该通知两次(或多次),它将打开该应用程序实例的次数。

即使我在AndroidManifest.xml中使用 android:launchMode =“ singleInstance” 定义了应用标签,也会发生这种情况。

我该怎么办,通知只会对第一次点击做出反应,或者仅出现一个应用实例?

1 个答案:

答案 0 :(得分:1)

将意图传递给待定意图的方式可能是问题所在。考虑以这种方式建立您的意图:

    Intent intent = new Intent(context.getApplicationContext(), <Activity to launch>.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra("start", "fullsystem");

实际上是通过代码调用来启动整个应用程序,这就是为什么它创建了应用程序的多个实例的原因。

您应该启动应用程序的特定部分,即应用程序的输入活动。