我使用以下代码将我的应用设置为默认程序。按 home
键转到我的应用...
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
它还可以使用 DISABLE_KEYGUARD
直接启动到我的应用程序中,无需解锁手机。
如何以编程方式改回默认启动器?意思是,我怎样才能返回到 Android 主屏幕?
我尝试使用 System.exit(0)
但它不起作用 - 它只是返回到我的应用程序而不是 android 主屏幕。
以下是我的代码。 它会自动返回到我的APP。 请指出代码中的任何问题。
TesthomeActivity.java
public class TesthomeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnTouchListener(exitappTouchListener);
}
OnTouchListener exitappTouchListener= new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction() == MotionEvent.ACTION_DOWN){
}
if(arg1.getAction() == MotionEvent.ACTION_UP ){
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
TesthomeActivity.this.startActivity(i);
finish();
System.exit(0);
}
return false;
}
};
}
StartupReceiver.java
public class StartupReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
Intent activityIntent = new Intent(context, TesthomeActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inno.testhome"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<activity android:name=".TesthomeActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
<receiver android:name="StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
最佳答案
阅读此 Is quitting an application frowned upon?
或者使用这个
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
yourActivity.this.startActivity(i);
finish();
我想这对你有帮助
关于java - 以编程方式返回启动器 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11076669/