如何完全关闭 activity 以便我可以阻止用户启动 activity
How to close activity completely so that I can prevent user to start the activity
我使用 AlarmManager 在特定时间启动 activity
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的代码,当activity启动时,它会播放警报声,如果用户点击屏幕,警报将被解除。
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();
}
}
当这个activity关闭时,它仍然在最近的应用程序列表中,如果用户点击最近的应用程序,这个activity会再次启动并播放声音
这个我也试过了,但是没用
android.os.Process.killProcess(android.os.Process.myPid());
AlarmActivity 只能由 AlarmManager 启动,用户不应启动它。我怎样才能完全关闭我的应用程序?
在您的 activity
的清单中使用 android:excludeFromRecents="true"
http://developer.android.com/guide/topics/manifest/activity-element.html#exclude
您可以使用以下标志:
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);
希望对您有所帮助。
我使用 AlarmManager 在特定时间启动 activity
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的代码,当activity启动时,它会播放警报声,如果用户点击屏幕,警报将被解除。
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();
}
}
当这个activity关闭时,它仍然在最近的应用程序列表中,如果用户点击最近的应用程序,这个activity会再次启动并播放声音
这个我也试过了,但是没用
android.os.Process.killProcess(android.os.Process.myPid());
AlarmActivity 只能由 AlarmManager 启动,用户不应启动它。我怎样才能完全关闭我的应用程序?
在您的 activity
的清单中使用android:excludeFromRecents="true"
http://developer.android.com/guide/topics/manifest/activity-element.html#exclude
您可以使用以下标志:
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);
希望对您有所帮助。