java - 以编程方式返回启动器 android

标签 java android android-intent

我使用以下代码将我的应用设置为默认程序。按 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/

相关文章:

java - 谷歌播放 .为什么我有 OpenGL ES 2.0+ 而不是 1.0

java - 您如何在 Java 中使用 Google App Engine 数据存储中的列表属性?

Java 8 函数式接口(interface),无参数,无返回值

android - 我应该将隐私政策副本放入应用程序本身还是仅放在 google play 商店列表中?

android - 在 AlertDialog.Builder 中格式化文本

android - 如何在 actionhandler 中获取对 Activity 类的引用

android - Android中这两种拍照方式有什么区别?

Java - 类中的重复方法以及如何从另一个类中调用它们

android - 从 Activity 外部处理 onActivityResult

android - 如何在 Android 上使用 Intent 打开关于 :reader? url=example.com 的 Firefox