AlarmManager无法启动我的Receiver

时间:2014-01-04 08:51:32

标签: java android broadcastreceiver alarmmanager

我正在设置AlarmaManager个活动,但我的BroadcastReceiver无法抓住它:

String task_uuid = UUID_Generator.getUUID();
Intent task_intent = new Intent(getApplicationContext(),AlarmReciever.class);
task_intent.putExtra("task_id", task_uuid);
task_intent.putExtra("test", 10101010);
PendingIntent operation = 
PendingIntent.getActivity(getBaseContext(),0, task_intent, 0);
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
long alarm_time = dateTime.getTimeInMillis();
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

AlarmReceiver

    public class AlarmReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        // TODO Auto-generated method stub
        System.out.println("test!!!");
        try {
            SMSUtills.sendSMS("0504235325", "נסיון תזמון");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

3 个答案:

答案 0 :(得分:0)

添加:

startActivity(task_intent);

答案 1 :(得分:0)

我在我的项目中使用了这段代码,它的工作正常:

Intent myIntent1 = new Intent(PrayerTimeActivity.this, AlarmReceiverFri.class); 
                    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);                    
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(PrayerTimeActivity.this,0, myIntent1, 0); 

                    alarmManager1.set(AlarmManager.RTC_WAKEUP,Calendar_Object.getTimeInMillis(), pendingIntent1);

接收器类是:

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiverFri extends BroadcastReceiver {

     @Override
     public void onReceive(Context context, Intent intent) {
     // When our Alaram time is triggered , this method will be excuted (onReceive)
     // We're invoking a service in this method which shows Notification to the User


      Intent myIntent = new Intent(context, NotificationServiceFri.class);
      context.startService(myIntent);
    }

} 

并且在AndroidManifest.xml中也包含

<receiver android:name=".AlarmReceiverFri" />   

感谢!!!

如果我的回答对您有所帮助,那么请不要忘记接受这个答案。 :)

答案 2 :(得分:0)

我的问题是我没有在清单中指定唤醒锁定权限。