【发布时间】:2020-09-07 21:44:33
【问题描述】:
在我的 Github 帐户上,我做了一个简单的项目来描述我的问题here my project
我的主活动有一个按钮,其 onClick 方法调用后台服务。 Backgroundservice 等待 5 秒,然后触发通知。
这里是BackgroundService的代码(你可以在Github找到
@Override
protected void onHandleIntent(Intent intent) {//methode appelée en arrière plan
if (intent != null) {
try{
Thread.sleep(5000);
} catch (Exception e){
e.printStackTrace();
}
mNotificationsManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
CharSequence tickerText = "view hike";
Intent intentReceiverAcctivity = new Intent(TAG_INTENT);
intentReceiverAcctivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intentReceiverAcctivity, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setAutoCancel(false);
builder.setTicker(tickerText);
builder.setContentTitle("The Hike");
builder.setContentText("HikeMap is available");
builder.setSmallIcon(R.drawable.explore);
Bitmap large_icon_bmp = BitmapFactory.decodeResource(this.getResources(),
R.drawable.ic_action_map);
builder.setLargeIcon(large_icon_bmp);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("Click on this icon to access the Hike's map"); //API level 16
builder.setNumber(5000); //durée d'apparition de l'icone dans la barre de notifications ?
builder.build();
Notification notification = builder.getNotification();
mNotificationsManager.notify(11, notification);
}
}
展开通知栏并单击 large_icon 应该会启动第二个名为 ReceiverActivity 的 Activity,其唯一目的是显示不同的消息。
在模拟器上,小图标出现在通知栏上,当我展开该栏时,我可以看到大图标,但单击它不会触发 ReceiverActivity
在我的智能手机上,通知栏上没有小图标,扩展通知区域上没有大图标...
知道服务代码出了什么问题吗? 这个小例子的完整代码可以在GitHub...上找到。
提前感谢您的帮助!!!
【问题讨论】:
标签: android android-activity android-service android-notifications