1

我使用以下代码将我的应用程序设置为默认程序。按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直接启动到我的应用程序中,无需解锁手机。

如何以编程方式更改回默认启动器?意思是,我怎样才能回到安卓主屏幕?

我尝试使用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;
    }
};
}

启动接收器.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>
4

3 回答 3

2

阅读此内容退出应用程序是否令人不悦?

或使用这个

Intent i = new Intent();
  i.setAction(Intent.ACTION_MAIN);
  i.addCategory(Intent.CATEGORY_HOME);
  yourActivity.this.startActivity(i); 
  finish(); 

我想这会对你有所帮助

于 2012-06-18T03:59:46.163 回答
0

您需要查询PackageManager响应 Home 意图的应用程序。

然后,您可以像开始任何其他活动一样启动正确的活动。请务必在处理之前从列表中删除您的应用...

有关这方面的有用信息,我建议 @Commonsware 的 Launchalot 示例应用程序,它显示了如何枚举响应特定意图的应用程序列表。

在这里查看他在 github 上的代码。

于 2013-09-04T11:06:58.127 回答
0

如果您的应用设置为默认启动器,Android 系统会在您的应用退出时自动重启。它不再有任何原始主屏幕的概念——就操作系统而言,您的应用现在就是主屏幕。

这就是为什么您无法通过退出应用程序返回到默认启动器的原因。获得所需结果的唯一方法是更改​​默认启动器

于 2012-09-06T10:32:22.683 回答