如果我打电话给System.exit()对手有什么不同killProcess().
我只对差异感兴趣
如何在应用程序退出时执行一些代码?我想在应用程序退出时删除临时数据.通过应用程序退出我的意思是应用程序没有在后台运行最小化,它完全消失.
我试图使服务在单独的进程中运行,服务正在检查应用程序进程是否未运行它应该删除临时文件夹.使用此方法,临时文件夹并不总是被删除,因为进程仍以最低优先级运行.
我无法在OnDestroy()中执行此操作.
服务代码:
[Service(Process = "com.lobomonitor")]
[IntentFilter(new string[] { "com.androidgui.ProcessMonitorService" })]
public class ProcessMonitorService : Service
{
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Thread processThread = new Thread(() =>
{
ActivityManager actManager = (ActivityManager) this.GetSystemService(ActivityService);
while (true)
{
Thread.Sleep(1000);
IList<ActivityManager.RunningAppProcessInfo> procList = actManager.RunningAppProcesses;
procList.Any(i => i.ProcessName.Equals("com.androidgui"));
if (!procList.Any(i => i.ProcessName.Equals("com.androidgui")))
{
FolderManager.Singleton.DeleteTempFolder();
break;
}
}
});
processThread.Start();
return StartCommandResult.RedeliverIntent;
}
public override IBinder OnBind(Intent intent)
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 桌面应用程序开发中的哪些概念(例如Java,WPF,Cocoa)最接近Android的基本概念,如Activity,ActivityGroup和Intent?(它们有何不同之处有什么细微差别?)
可能的重复:
退出应用程序 - 这是否令人不悦?
我编写了一个应用程序,并覆盖了 onBackPress() 方法:
@Override
public void onBackPressed() {
Log.e("Main Activity", "onBackPressed!");
exitAlert.show();//when the user click the ok button it will call the MainActivity.this.finish();
}
Run Code Online (Sandbox Code Playgroud)
但是我发现当finish()函数被调用时,我仍然可以在“设置->应用程序->运行服务”中找到这个应用程序。所以我必须在这里手动停止它。我不明白为什么它在被调用时仍然运行finish()。如何通过单击对话框销毁我的应用程序?提前致谢!
我有一个测验应用程序,它有一个计时器用于整个游戏活动,你应该在指定的分钟内回答尽可能多的问题.
在指定的分钟结束后,它将带您进入显示您的分数的结果活动.在背面按下时,我创建了一个警告对话框,询问您是否要返回主菜单.如果单击是,则页面应返回主菜单并停止/终止游戏活动.
但是,当我单击"是"时,它将返回主菜单,但当您仍然在应用程序的任何位置时,结果仍将显示我之前的游戏活动.也许我还没有真正完成游戏活动,..这是我的活动中的计时器和背压片段:
new CountDownTimer(seconds, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Seconds left: " + millisUntilFinished / 1000);
}
public void onFinish() {
Intent intent = new Intent(GameActivityAddition.this, Score.class);
intent.putExtra("totalscore", score);
intent.putExtra("numberquestions", rowId);
intent.putExtra("d", difficulty);
db.close();
startActivity(intent);
}
}.start();
@Override
public void onBackPressed() {
AlertDialog.Builder abuilder = new Builder(GameActivityAddition.this);
abuilder.setMessage("Return to Main Menu?");
abuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent main = new Intent(GameActivityAddition.this, Main.class);
startActivity(main);
finish();
}
});
abuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public …Run Code Online (Sandbox Code Playgroud) 我试过 System.Exit(0),但不是我需要的;
我正在尝试安全地销毁 android 应用程序并从内存中清除它,
我可以制作一个按钮来退出 android 应用程序并从内存中清除所有应用程序吗?
我正在创建一个有一些线程的应用程序,我想关闭它。我试过:
System.exit(0);
Run Code Online (Sandbox Code Playgroud)
但应用程序会自行重启。
请帮帮我,谢谢。
编辑:
OnDestroy 代码:
@Override
public void onDestroy(){
android.os.Process.killProcess(android.os.Process.myPid());
}
Run Code Online (Sandbox Code Playgroud)
OnOptionsItemClicked:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
startActivity(new Intent(this, ConnActivity.class));
finish();
break;
case R.id.item2:
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
break;
case R.id.item3:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Mensaje al servidor");
alert.setMessage("Enviar mensaje al servidor");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Enviar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString(); …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我总是希望用户从启动画面开始.例如,我的应用程序可能在后台打开,并弹出一些启动启动活动的通知.这应该终止所有以前正在运行的活动.
我通过存储所有正在运行的活动参考的列表来完成此操作.当启动启动时,它只是调用
for(runningActivity : runningActivitiesList) {
runningActivity.finish();
}
Run Code Online (Sandbox Code Playgroud)
此解决方案效果很好.但是,Android Studio在存储对活动的引用时会向我发出内存泄漏警告.
有人可以建议我一个更好的方法,以避免内存泄漏?
我正在使用Application类来共享活动中的全局变量,我在onCreate应用程序类的方法中设置它们.当我启动app变量时,在onCreate中设置值,并且在活动中使用app时,我正在更改varables的值.当我退出应用程序并再次启动它时,我将获取旧值,即活动中设置的变量的最后值.这意味着onCreate of Application不再在启动应用程序上运行.这是Application类的onCreate方法中的代码.
@Override
public void onCreate() {
super.onCreate();
application = this;
category = 12;
subCategory =22;
}
Run Code Online (Sandbox Code Playgroud)
看起来旧的应用程序对象仍在内存中,并且在第二次启动应用程序时不会调用onCreate.
需要做的是使onCreate应用程序类再次运行或在应用程序类中初始化变量的位置,以便每次都运行代码.
我在我的应用程序中使用了这里的代码,当我点击"取消"选项时,应用程序只是最小化,当我长按住主页按钮并选择应用程序时.它显示相同的屏幕而不是闪屏或通常的方式应用程序启动 - 所以我假设它只是带我到主屏幕.我正在寻找一种可以完全关闭主要活动的方法.
我尝试了以下方法,每次都崩溃了:
finish();mainactivity.class.finish();systemexit()setresult() 在另一项活动中.