我有以下代码来设置闹钟
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
int [] progressVals = getPackets();
displayList(progressVals, values);
setAlarm();
}
private void setAlarm(){
DatabaseHandler db = new DatabaseHandler(this);
Queue top = db.getTopQueue();
if(top != null){
String p = top.getPacket();
double timestamp = top.getTimestamp();
int qNum = top.getExNum();
Intent intent = new Intent("com.dejalearn.algorithm.AlarmReciever.class");
PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayAllActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar instance = Calendar.getInstance();
instance.add(Calendar.SECOND, 3);
alarmManager.set(AlarmManager.RTC_WAKEUP, instance.getTimeInMillis(), pendingIntent);
Toast.makeText(DisplayAllActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
Log.i("DISPLAY ALL", "ALARM SET UP");
}
}
从此我尝试从实际运行的活动中运行的活动称为AlarmReciever,它扩展了brodcast接收器,但是我的报警接收器中的toast永远不会运行。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class AlarmReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("FOUND", "THE MESSAGE");
try{
Toast.makeText(context, "made it", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
为什么我的新烤面包永远不会运行。我看了几个例子,但似乎没有任何工作。我不确定是什么问题。
答案 0 :(得分:3)
将您的意图更改为有效意图:
Intent intent = new Intent(DisplayAllActivity.this, AlarmReciever.class);
确保将接收器添加到AndroidManifest.xml
,如下所示:
<receiver name=".AlarmReciever" />
答案 1 :(得分:1)
以下是我Alarm application
中的一个例子:
<强> myAlarmActivity.class 强>
long startTimeInMilli=setTimeInMilli-System.currentTimeMillis();
Log.d("MYAPP", "set time in milli"+setTimeInMilli);
Log.d("MYAPP", "Total time in milli:"+startTimeInMilli);
Intent intent = new Intent();
intent.setClass(AlarmMgrActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmMgrActivity.this, 234324243/*some random id*/, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ startTimeInMilli, pendingIntent);
Toast.makeText(getBaseContext(), "Alarm set in " + startTimeInMilli/1000+ " seconds", Toast.LENGTH_SHORT).show();
以下是myBroadcastReceiver的代码:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
//Toast.makeText(arg0, "Don't panik but your time is up!!!!.",
// Toast.LENGTH_LONG).show();
// Vibrate the mobile phone
/*Vibrator vibrator = (Vibrator) arg0.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(10000);*/
AudioManager am= (AudioManager)arg0.getSystemService(Context.AUDIO_SERVICE);
if(AlarmMgrActivity.ToMode.equalsIgnoreCase("normal"))
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
else if(AlarmMgrActivity.ToMode.equalsIgnoreCase("silent"))
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
else if(AlarmMgrActivity.ToMode.equalsIgnoreCase("vibrate"))
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Intent i=new Intent();
i.setClass(arg0, MyService.class);
arg0.startService(i);
}
}
在清单中,我已在Manifest.xml中注册了我的广播接收器,如下所示:
<application>
.....
.....
<receiver android:name="MyBroadcastReceiver" />
.....
.....
</application>