与广播接收器一起使用的PendingIntent不工作

时间:2013-03-31 18:28:39

标签: android broadcastreceiver alarmmanager

我要注册一个BroadcastReceiver,并在收到广播时传递一个我想要显示的id。

我想要做的是访问特定实例(即警报响起时)的数据库条目,这就是我通过意图传递id的原因

View.OnClickListener saveHandler = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Log.d("Insert: ", "Inserting ..");

        mContext = getApplicationContext();
        objalarmManger = (AlarmManager) getSystemService(ALARM_SERVICE);

        Intent broadcastIntent = new Intent(mContext,
                MyBroadCastReceiver.class);
        broadcastIntent.putExtra("id",22);
        PendingIntent objPI = PendingIntent.getBroadcast(mContext, 0,
                broadcastIntent, 0);

        Calendar objC = Calendar.getInstance();
        objC.set(yearData, monthData, dayData, hour, min);
        objalarmManger.set(AlarmManager.RTC_WAKEUP, objC.getTimeInMillis(),
                objPI);
    }
};

广播接收器代码是

public class MyBroadCastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {

    int id = arg1.getIntExtra("id", 0);

    Toast.makeText(arg0, id, Toast.LENGTH_LONG).show();
    Log.d("Received", id + "");
}

}

我无法看到任何log cat entiers,代码实现中是否有任何错误。 任何帮助将不胜感激:)

0 个答案:

没有答案