1

即使应用程序关闭,我也想使闹钟运行, 当应用程序未关闭时,一切正常, 但当触发时间并关闭应用程序时,时间到了应用程序崩溃的时候。 我尝试了一些解决方案,但他们没有帮助。当应用程序关闭时广播接收器

这里是我的代码:

public class MainActivity extends Activity { 

//used for register alarm manager 
PendingIntent pendingIntent; 
//used to store running alarmmanager instance 
AlarmManager alarmManager; 
//Callback function for Alarmmanager event 
BroadcastReceiver mReceiver; 

TimePicker tp; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Register AlarmManager Broadcast receive. 
    RegisterAlarmBroadcast(); 

    tp = (TimePicker)findViewById(R.id.timePicker1); 
    tp.setIs24HourView(true); 

    Calendar cal=Calendar.getInstance(); 
    tp.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY)); 
    tp.setCurrentMinute(cal.get(Calendar.MINUTE)); 

} 

public void onClickSetAlarm(View v) 
{ 
    //Get the current time and set alarm after 10 seconds from current time 
    // so here we get 
    Calendar time = Calendar.getInstance(); 
    time.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour()); 
    time.set(Calendar.MINUTE, tp.getCurrentMinute()); 
    time.set(Calendar.SECOND, 0); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent); 
    tp.setEnabled(false); 
// alarmManager.set(AlarmManager.RTC_WAKEUP, 
//   System.currentTimeMillis() + Integer.parseInt(time.getText().toString()) * 1000 , pendingIntent); 
} 

private void RegisterAlarmBroadcast() 
{ 
    // Log.i("Alarm Example:RegisterAlarmBroadcast()", "Going to register Intent.RegisterAlramBroadcast"); 

    //This is the call back function(BroadcastReceiver) which will be call when your 
    //alarm time will reached. 
    mReceiver = new BroadcastReceiver() 
    { 
     private static final String TAG = "Alarm Example Receiver"; 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      //Log.i(TAG,"BroadcastReceiver::OnReceive() >>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 
      Toast.makeText(context, "Congrats!. Your Alarm time has been reached", Toast.LENGTH_LONG).show(); 
      tp.setEnabled(true); 

      // define sound URI, the sound to be played when there's a notification 
      Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

      // intent triggered, you can add other intent for other actions 

      PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent("MY_ALARM_NOTIFICATION").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0); 

      // this is it, we'll build the notification! 
      // in the addAction method, if you don't want any icon, just set the first param to 0 
      Notification mNotification = new Notification.Builder(MainActivity.this) 

       .setContentTitle("New Post!") 
       .setContentText("Here's an awesome update for you!") 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentIntent(pIntent) 
       .setSound(soundUri) 

       // .addAction(R.drawable.ninja, "View", pIntent) 
       .addAction(0, "Remind", pIntent) 
       .build(); 

      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

      // If you want to hide the notification after it was selected, do the code below 
      // myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 

      notificationManager.notify(0, mNotification); 
     } 
    }; 

    // register the alarm broadcast here 
    registerReceiver(mReceiver, new IntentFilter("MY_ALARM_NOTIFICATION")); 
    pendingIntent = PendingIntent.getBroadcast(this, 123456789, new Intent("MY_ALARM_NOTIFICATION").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),0); 
    alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
    } 
} 
private void UnregisterAlarmBroadcast() 
{ 
    alarmManager.cancel(pendingIntent); 
    getBaseContext().unregisterReceiver(mReceiver); 
} 
@Override 
protected void onDestroy() { 
    unregisterReceiver(mReceiver); 
    super.onDestroy(); 
    } 
} 

任何形式的帮助,将不胜感激。

这里是我的manifest.xml:

<receiver android:name=".MainActivity" > 

     <intent-filter> 
      <action android:name="MY_ALARM_NOTIFICATION" /> 
     </intent-filter> 

    </receiver> 
+0

它究竟如何崩溃?请发布logcat转储。 – 2014-10-20 20:22:59

回答

1

您已将清单中的Activity定义为<receiver>。这是行不通的。

这里有几个问题。

  1. 您的BroadcastReceiver必须在清单中声明为<receiver>标记。这意味着您用作BroadcastReceiver的课程必须是公开的。
  2. 在您的代码中,您已将BroadcastReceiver声明为匿名类。一般来说,这将起作用,但如果BroadcastReceiver需要从应用程序之外的组件调用,则这不起作用。 AlarmMnager在您的应用程序之外运行(实际上,它甚至会在应用程序未运行时触发警报),因此您的BroadcastReceiver必须定义为扩展BroadcastReceiver的公共类。
  3. 您正在致电PendingIntent.getActivity()。如果你想开始BroadcastReceiver,那么你需要拨打PendingIntent.getBroadcast()
  4. 在您的onReceive()方法中,您正在使用的成员变量是null,当您的应用程序未运行时触发警报。由于即使您的应用程序未运行,AlarmManager也会触发警报,因此在调用onReceive()时,不能指望已设置任何成员变量

我建议你看看如何做到这一点的一些例子。有很多可用的。

2

甚至没有看到堆栈跟踪,我会猜测,AlarmManager试图提供警报,但由于您的应用程序已被杀害和Receiver你注册已被杀害,NullPointerExceptionAlarmManager中的某处。根据设计,当您知道您的Activity正在远离(例如在onStop())时,您必须取消注册/删除警报。如果你想让你的报警器工作时,你的应用程序是不可见的和/或它的封闭后:

  1. 你需要在你AndroidManifest.xml注册的接收器。在应用程序离开导航后,应用程序内以编程方式注册的接收者不应该活动 - 并且如果用户退出应用程序,则会与应用程序一起销毁。

  2. 您可能还想在您的接收器中注册BOOT_COMPLETED。当设备重新启动时,所有报警丢失,因此无论何时重新启动设备,您都需要重新设置报警(检查SharedPreferences标志以查看是否应该有一组报警)。

+0

感谢的对你有所帮助,这里是我的清单: <接收器的android:NAME = “MainActivity。”> <意图过滤器> <操作机器人:名字= “MY_ALARM_NOTIFICATION”/> zb22 2014-10-21 08:14:25

相关问题