我使用AlarmManager在特定时间开始活动
Intent intent = new Intent(getApplicationContext(),AlarmActivity.class);
PendingIntent sender = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 24);
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
这是AlarmActivity的代码,当活动开始时,它会播放闹钟声,如果用户点击屏幕,闹钟就会被解除。
public class AlarmActivity extends Activity {
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
View v =((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(true);
mediaPlayer.setDataSource(this, notification);
mediaPlayer.prepare();
mediaPlayer.start();
}
catch(Exception e)
{
}
}
public void onDestroy() {
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
}
}
当此活动关闭时,它仍然在最近的应用列表中,如果用户点击最近的应用,此活动将启动并再次播放声音
我也尝试了这个,但它没用了
android.os.Process.killProcess(android.os.Process.myPid());
AlarmActivity必须仅由AlarmManager启动,用户无法启动它。如何完全关闭我的应用程序?
答案 0 :(得分:4)
在活动清单中使用android:excludeFromRecents="true"
http://developer.android.com/guide/topics/manifest/activity-element.html#exclude
答案 1 :(得分:0)
您可以使用以下标志:
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
您需要设置
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
下面
Intent intent = new Intent(getApplicationContext(),AlarmActivity.class);
希望它对你有所帮助。