我遇到Android Notification的操作问题。
这是我的想法:
我的BroadcastReceiver
在广播时醒来,然后创建通知。
该通知应该:
MainActivity
我在点击通知时设法让ACTION_SEND
正常工作,但我对action
没有运气......
在网络上找不到任何内容,只有pendingIntents
打开活动。
这是我的代码:
public class AlarmReceiver extends BroadcastReceiver {
static int notificationId = 0;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AlarmReceiver","ricevuto " + intent.getAction());
Calendar calendar = Calendar.getInstance();
String tipo = "sconosciuto";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(R.drawable.ic_flag_black_24dp);
if(intent.getAction().equals(Constants.ALARM_ACTION_PALINDROM)){
tipo="palindromo";
}
if(intent.getAction().equals(Constants.ALARM_ACTION_DOUBLE)){
tipo="simmetrico";
}
mBuilder.setContentTitle("Orario " + tipo + "!");
String currentTime = new SimpleDateFormat("HH:mm").format(new Date());
mBuilder.setContentText(currentTime);
Intent shareIntent = new Intent();
Intent notifIntent = new Intent(context,MainActivity.class);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Sono le " + currentTime +", " + tipo + "\u2764");
Intent shareIntentWithChooser = Intent.createChooser(shareIntent,"Send to");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notifIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentShare = PendingIntent.getBroadcast(context,notificationId,shareIntentWithChooser,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.ic_send_black_18dp,"Condividi",pendingIntentShare);
mBuilder.setContentIntent(pendingIntent);
Notification notification = mBuilder.build();
notification.flags = Notification.DEFAULT_LIGHTS|Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId,notification);
notificationId++;
}
}
在此先感谢,我真的无法理解它!
答案 0 :(得分:2)
您应该使用getBroadcast()
代替PendingIntent
来创建Activity
,因为您希望使用ACTION_SHARE
启动{{1}}而不发送广播。