在MainActivity中我有:
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
导致关闭应用程序但是在我添加了这个类之后,通过按住主页按钮仍然可以使用该应用程序:
package com.example.smbp1;
import android.app.Application;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.Log;
public class MyApplication extends Application {
public static int backColorRGB = 0;
@Override
public void onCreate() {
super.onCreate();
Log.i("APLIKACJA", "MTK");
usePreferences();
}
public void usePreferences(){
SharedPreferences settings = getSharedPreferences(OptionListActivity.MY_PREFERENCES, MODE_WORLD_READABLE);
String fontColorAsString = settings.getString(getResources().getString(R.string.font_color), "0");
int fColorRGB = 0;
if (fontColorAsString.equals("RED"))
fColorRGB = Color.RED;
else if (fontColorAsString.equals("BLUE"))
fColorRGB = Color.BLUE;
else if (fontColorAsString.equals("GREEN"))
fColorRGB = Color.GREEN;
Log.i("TRY TO READ", "TRY TO READ");
//background;
String backColorAsString = settings.getString(getResources().getString(R.string.background_color), "0");
Log.i(getResources().getString(R.string.font_color), backColorAsString);
if (backColorAsString.equals("RED"))
backColorRGB = Color.RED;
else if (backColorAsString.equals("BLUE"))
backColorRGB = Color.BLUE;
else if (backColorAsString.equals("GREEN"))
backColorRGB = Color.GREEN;
}
}
并添加到清单:
<application
android:name="com.example.smbp1.MyApplication"....
点击后退按钮后,应用程序不会关闭。如何让它再次运作?
答案 0 :(得分:0)
TRY
@Override
public void onBackPressed()
{
android.os.Process.killProcess(android.os.Process.myPid());
}
这是我尝试过的最佳解决方案。
这和上面一样,它立即终止了Linux进程和应用程序的所有线程:
@Override
public void onBackPressed()
{
System.exit(0);
}