我面临一些未决意图的问题。我已经使用通知管理器设置了一些待处理意图。当用户点击这些通知时,它们会启动一个活动。我在未决意图中使用了一些额外的意图。单击通知即可正常工作。
Notification notification = new Notification(icon, tickerText, when);
    Context context = getApplicationContext();
Intent intentOptionDialog = new Intent(Safety_Check_Service.this, Dialog_Safety_Check.class);
    intentOptionDialog.putExtra("startID",startId);
    intentOptionDialog.putExtra("CheckInID", CheckInId);
    intentOptionDialog.putExtra("Frequency", Frequency);
    intentOptionDialog.putExtra("flagFirstShedule", true);
        stopID = (startId + 17);
    intentOptionDialog.putExtra("stopID", stopID);
    PendingIntent contentIntent = PendingIntent.getActivity(Safety_Check_Service.this, DIALOG_ID, intentOptionDialog, 0);
但我的问题是我想从另一个活动启动这些待处理的意图。挂起的意图已创建。我如何从另一个活动中启动这些挂起的意图,以及如何获取使用挂起的意图设置的额外内容?
请帮我。
我在IntentService中运行后台任务.我在通知栏中显示进度(使用NotificationManager),如果有人点击该通知,则要取消该任务.
我用广播服务成功实现了它.但是,我在想是否有可能对localBroadcast做同样的事情.
如果我将"registerReceiver"更改为"LocalBroadcastManager.getInstance(this).registerReceiver",则代码不起作用.你能否告诉我为什么我不能使用localbroadcast和pendingintent?
谢谢,
目前我的代码是:
boolean isCancelled;
Context context;
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
int mId = 2;
protected void onHandleIntent(Intent intent) {
    isCancelled = false;
    context = getApplicationContext();
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.migrationdesk.mylibman.MyServiceClass.STOP");
    registerReceiver(receiver, filter);
    getNotification();
    mainTask(); // THIS IS SOME BACKGROUND TASK
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
                if(action.equals("com.migrationdesk.mylibman.MyServiceClass.STOP")){
                    isCancelled = true;
                }
    }
};
protected void getNotification() {
    Intent newintent = new …android broadcast android-pendingintent localbroadcastmanager
我从这份文件中复制并粘贴并尝试过PutExtra.
我拍了拍button1,button2并且button3,然后从挖掘的通知button1,但ResultActivity开始用button3,为什么呢?
我希望被显示为button1.你知道解决方案吗?
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button).setOnClickListener(listener);
        findViewById(R.id.button2).setOnClickListener(listener);
        findViewById(R.id.button3).setOnClickListener(listener);
    }
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    notifyNotification("button1");
                    break;
                case R.id.button2:
                    notifyNotification("button2");
                    break;
                case R.id.button3:
                    notifyNotification("button3");
                    break;
            }
        }
    };
    private int mId = 0;
    private void notifyNotification(String value) { …android android-intent android-notifications android-pendingintent
我试图使用PendingIntent将自定义的Serialized对象从我的IntentService传递给BroadcastReceiver.
这是我的自定义对象:
Row.java
public class Row implements Serializable {
    private String name;
    private String address;
    public Row(BluetoothDevice device) {
        this.name = device.getName();
        this.address = device.getAddress();
    }
}
这是我的IntentService
MyIntentService.java
public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    protected void onHandleIntent(Intent workIntent) {
        AlarmManager alarmMgr;
        PendingIntent alarmPendingIntent;
        Intent alarmIntent;
        // The object "RowsList" is passed from my MainActivity and is correctly received by my IntentService.
        // NO PROBLEMS HERE
        Row[] …java android android-pendingintent android-broadcastreceiver
我想在点击推送通知消息时打开特定片段。在我的情况下,当通知发生时片段是打开的。但我想在点击通知消息而不是到达消息时打开片段。
这是我的 sendNotification() 方法:-
 private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("data", "fromoutside");
        getApplicationContext().startActivity(intent);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(getNotificationIcon())
                .setContentTitle("Telepoh")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    } 
这是我的 onNewIntent() 方法,它在我打开片段的 MainActivity 中覆盖:-
@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Fragment fragment = new NotificationActivity();
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).addToBackStack(null).commit();
    }
我的问题是我想在点击推送通知消息时打开这个片段,但在我的情况下,当推送通知消息到达时片段是打开的。
android push-notification android-intent android-pendingintent google-cloud-messaging
我有以下从服务创建的代码以进行通知。我试图额外发送不同的值,每次在接收器中仍然获得最后一个设置值。
Intent closeButton = new Intent(this,DownloadCancelReceiver.class);
closeButton.putExtra("id",id);
closeButton.setAction("Action"+Long.toString(id));
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, id, closeButton, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView = new RemoteViews(getPackageName(), R.layout.notification_view);
notificationView.setProgressBar(R.id.pb_progress, 100, 5, false);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);
我正在学习 android 服务。我的主要活动中有一个按钮,单击该按钮后,将使用媒体播放器开始播放声音文件,并显示通知。我希望在单击通知时停止音乐服务并删除通知。我已经把头撞在墙上几个小时了,因为我无法弄清楚我做错了什么。这是我的服务类别:
public class MusicService extends Service {
public MusicService() {
}
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
    super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MediaPlayer player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
    player.setLooping(true);
    player.start();
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("Hello") …android android-service android-notifications android-pendingintent
我有一个警报接收器,它会做一些检查,然后Notification为每个检查创建一个。这意味着它可以创建多个Notification. 这一切正常。但是,我已Intent连接到通知以在点击通知时启动活动。
这是通知代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_OUTPUT_LEVELS)
                .setSmallIcon(icon)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(longmessage))
                .setContentIntent(getPendingIntent(solarEdge, reason))
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId is a unique int for each notification that you must define
// we use the installation's ID to make sure all notifications get sent
notificationManager.notify(solarEdge.getInfo().getId(), mBuilder.build());
创建的方法PendingIntent是:
private PendingIntent getPendingIntent(SolarEdge solarEdge, int reason) {
    String apikey = solarEdge.getApikey();
    int installationId = solarEdge.getInfo().getId();
    Intent intent = new Intent(context, …我已经在我的应用程序中使用通知有一段时间了,一切都工作正常,直到我最近瞄准了 android 12。现在,当我在 android 12 上收到通知时,我的应用程序崩溃了。但在低于 android 12 的设备上一切工作正常。这是我到目前为止所尝试过的。
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
private static final String CHANNEL_ID = "1";
private static final String CHANNEL_NAME = "Notifications";
@Override
public void onReceive(Context context, Intent intent) {
    // For android 8 and above
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(context.getString(R.string.notifications));
        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setLightColor(Color.RED);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);  //Show notification on lock screen
        NotificationManager manager = context.getSystemService(NotificationManager.class);
        assert manager != null;
        manager.createNotificationChannel(channel);
    }
    Cursor cursor = …android broadcastreceiver android-notifications android-pendingintent android-12
我有一个运行以下代码的Activity(定义了时间和间隔):
Intent buzzIntent = new Intent(getBaseContext(), BuzzReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, buzzIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
buzzIntent.putExtra("interval", interval);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, interval * 60 * 1000, pendingIntent);
和一个具有以下onReceive的BroadcastReceiver:
@Override
public void onReceive(Context context, Intent intent) {
    try {
        int interval = intent.getIntExtra("interval", -1);
        <... more code ...>
    } catch (Exception e) {
        e.printStackTrace();
    }
}
但是intent.getIntExtra()返回-1(这不是我检查的Activity中的interval值),因此由于某种原因,BroadcastReceiver没有获得我存储在Activity中的intent中的额外内容.
我尝试过很多不同的东西,但似乎没什么用.我在这里错过了什么吗?
android broadcastreceiver android-intent android-pendingintent