我使用以下代码进行警报,它会在10秒后发出警报。它在模拟器中运行良好,但在真实设备中它的强制关闭..你能以正确的方式引导我。 这是我的行为......
public class Alarm1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,001000,intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
Toast.makeText(this,"Alarm set", Toast.LENGTH_LONG).show();
} }` 这是接收器类
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
try{
Intent back = new Intent(context,Alarm.class);
back.addFlags(Intent.FLAG_FROM_BACKGROUND);
back.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
back.setAction("android.intent.action.MAIN");
back.addCategory("android.intent.category.LAUNCHER");
context.startActivity(back);
}
catch(Exception e){}
}
}
这是播放铃声以进行闹钟。
public class Alarm extends Activity {
private MediaPlayer mp; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
mp = MediaPlayer.create(this, R.raw.up);
mp.start();
Button start = (Button) findViewById(R.id.Button01);
start.setOnClickListener(vvvvv);
}
private OnClickListener vvvvv = new OnClickListener() {
public void onClick(View v) {
mp.stop();
finish();
}
};
}
这在模拟器中工作正常但在真实设备上没有...我也使用了权限.. 我正在使用三星Galaxy 1.6。 提前谢谢..
答案 0 :(得分:0)
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND)+10 );
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);
答案 1 :(得分:0)
试试这个,当我遇到同样的问题时,它帮助了我
看起来像这样,
如果您需要进一步的帮助,请在此处评论您的错误日志。