getExtra从意图从pendingIntent推出 [英] getExtra from Intent launched from a pendingIntent

查看:129
本文介绍了getExtra从意图从pendingIntent推出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户选择大一些的时间从列表,使某些报警,并在给定的时间创建一个通知吧。我的问题是,showname一个putExtra在我的意图不能在广播接收机接收。它总是空值。 这是我做我的大多数意图的方式,但我认为这个时间,因为pendingIntent或BroadcastReceiver的东西可能需要被differentelly完成。 谢谢

发送的意图通过正在申请意向的功能

 公共无效setAlarm(字符串showname,字符串时间){

    的String [] hourminute = time.split(:);
    串小时= hourminute [0];
    串分钟= hourminute [1];
    日历RightNow公司= Calendar.getInstance();
    rightNow.set(Calendar.HOUR_OF_DAY,的Integer.parseInt(小时));
    rightNow.set(Calendar.MINUTE,的Integer.parseInt(分钟));
    rightNow.set(Calendar.SECOND,0);
    长T = rightNow.getTimeInMillis();
    长T1 = System.currentTimeMillis的();

    尝试 {

    意向意图=新的意图(这一点,alarmreceiver.class);
    捆绑C =新包();
    c.putString(showname,showname); //这是我想传递的价值
    intent.putExtras(C);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(本,12345,意向,0);

    AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,rightNow.getTimeInMillis(),pendingIntent);
    //Log.e("ALARM,米利斯的时间:+ System.currentTimeMillis的());
    Toast.makeText(这一点,报警设置,Toast.LENGTH_LONG).show();

    }赶上(例外五){
        Log.e(报警,错误code:+ e.toString());
    }
}
 

这是接收端

 公共类alarmreceiver扩展的BroadcastReceiver {

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    // Toast.makeText(背景下,报警的工作。,Toast.LENGTH_LONG).show();
    叠B = intent.getExtras();
    字符串showname = b.getString(showname); //这是我想接受它,但其空
    NotificationManager马槽=(NotificationManager)上下文
            .getSystemService(context.NOTIFICATION_SERVICE);

    通知通知=新的通知(R.drawable.icon,
            电视周刊Υπενθύμιση,System.currentTimeMillis的());
    PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
            新意图(上下文,tvguide.class),0);

    notification.setLatestEventInfo(上下文,ΤοΠρόγραμμαΞεκίνησε,
            showname,contentIntent);

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;

    notification.sound = Uri.parse(文件:///sdcard/dominating.mp3);
    notification.vibrate =新长[] {100,250,100,500};
    manger.notify(1,通知);
}
}
 

解决方案

如果你改变了额外的价值的意图,然后在创建挂起的意图,你应该使用标志PendingIntent.FLAG_CANCEL_CURRENT。

一个简单的例子是

  PendingIntent PI = PendingIntent.getBroadcast(背景下,0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT);
 

这是正确的方式,并确保你的新价值交付。

希望它帮助。

I am trying to make some alarms after the user selects something with a time from a list and create a notification for it at the given time. My problem is that the "showname" that a putExtra on my Intent cant be received at the broadcast receiver. It always get null value. This is the way I do it for most of my intents but I think this time maybe because of the pendingIntent or the broadcastReceiver something need to be done differentelly. Thank you

The function that sends the Intent through the pending intent

public void setAlarm(String showname,String time) {

    String[] hourminute=time.split(":");
    String hour = hourminute[0];
    String minute = hourminute[1];
    Calendar rightNow = Calendar.getInstance();
    rightNow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
    rightNow.set(Calendar.MINUTE, Integer.parseInt(minute));
    rightNow.set(Calendar.SECOND, 0);
    long t=rightNow.getTimeInMillis();
    long t1=System.currentTimeMillis();

    try {   

    Intent intent = new Intent(this, alarmreceiver.class);  
    Bundle c = new Bundle();            
    c.putString("showname", showname);//This is the value I want to pass
    intent.putExtras(c);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 12345, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis(),pendingIntent);
    //Log.e("ALARM", "time of millis: "+System.currentTimeMillis());
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Log.e("ALARM", "ERROR IN CODE:"+e.toString());
    }
}

And this is the receiving end

public class alarmreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();      
    Bundle b = intent.getExtras();
    String showname=b.getString("showname");//This is where I suppose to receive it but its null
    NotificationManager manger = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.icon,
            "TVGuide Υπενθύμιση", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, tvguide.class), 0);

    notification.setLatestEventInfo(context, "Το Πρόγραμμα Ξεκίνησε",
            showname, contentIntent);

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;

    notification.sound = Uri.parse("file:///sdcard/dominating.mp3");
    notification.vibrate = new long[]{100, 250, 100, 500};
    manger.notify(1, notification);
}           
}

解决方案

If you change the Extra's value in the intent, then while creating the pending intent you should use the flag PendingIntent.FLAG_CANCEL_CURRENT.

A simple example would be

PendingIntent pi = PendingIntent.getBroadcast(context, 0,intentWithNewExtras,PendingIntent.FLAG_CANCEL_CURRENT);

This is the right way and will ensure that your new values are delivered.

Hope it helps.

这篇关于getExtra从意图从pendingIntent推出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆