【问题标题】:Android AlarmManager Pass extra from activity to serviceAndroid AlarmManager 将额外的从活动传递到服务
【发布时间】:2015-01-31 07:35:46
【问题描述】:

我尝试了很多链接,但无论我的附加内容是什么,我都一直为空。首先,我有一个活动 (MainActivity),它将使用服务 (NotifyService) 设置通知。我可以让服务和通知在适当的时间出现。但是传递一个字符串似乎是个问题。

在 MainActivity onCreate() 我有:

 Intent myIntent = new Intent(this, NotifyService.class);
 //this is what I would like to pass over
 myIntent.putExtra("newtitle" ,"this is the new title");
 myIntent.putExtra("newcontent" ,"this is the new content");

 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

 Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.SECOND, 0);
 calendar.set(Calendar.MINUTE, 25);
 calendar.set(Calendar.HOUR, 11);
 calendar.set(Calendar.AM_PM, Calendar.PM);

 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);


 Toast.makeText(MainActivity.this, "Set Alarm", Toast.LENGTH_LONG).show();

在我的 NotifyService onCreate 我有:

Intent intent = new Intent(this.getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
//this will be null or give me errors every time
//        if (intent.getExtras().containsKey("newtitle"))
//            title=(String) intent.getExtras().get("newtitle");
//        if (intent.getExtras().containsKey("newcontent"))
//            content=(String) intent.getExtras().get("newcontent");

    NotificationCompat.Action action = new NotificationCompat.Action.Builder(
            R.drawable.basketball, getString(R.string.wearTitle), pendingIntent
    ).build();

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification notification = new NotificationCompat.Builder(this)
            .setContentText(content)
            .setContentTitle(title)
            .setSmallIcon(R.drawable.basketball)
            .setSound(sound)
            .extend(new NotificationCompat.WearableExtender().addAction(action))
            .build();

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
    notificationManagerCompat.notify(001, notification);

主要是我一直在关注这个教程:https://www.youtube.com/watch?v=tyVaPHv-RGo

我也看过这个链接:How can I correctly pass unique extras to a pending intent?

我已经用 PendingIntent 标志尝试了很多东西,但仍然没有运气。我已经看到它使用意图和服务完成,但没有使用待处理的意图。

非常感谢

【问题讨论】:

    标签: android-service android-notifications android-pendingintent android-alarms


    【解决方案1】:

    原来在创建时使用它,我必须使用 onStartCommand。代码如下:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Toast.makeText(this, "Starting..", Toast.LENGTH_SHORT).show();
        Log.d("0",intent.getStringExtra("newtitle"));
    
        title = intent.getStringExtra("newtitle");
        content = intent.getStringExtra("newcontent");
        Intent myintent = new Intent(this.getApplication(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,myintent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(
                R.drawable.basketball, getString(R.string.wearTitle), pendingIntent
        ).build();
    
        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        Notification notification = new NotificationCompat.Builder(this)
                .setContentText(content)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.basketball)
                .setSound(sound)
                .extend(new NotificationCompat.WearableExtender().addAction(action))
                .build();
    
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(001, notification);
        return PendingIntent.FLAG_UPDATE_CURRENT; // or whatever your flag
    }
    

    来自:Pass data from Activity to Service using an Intent

    【讨论】:

    • 它说我不能接受自己的答案。我试过了抱歉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多