有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java以编程方式返回到launcher 安卓

我使用以下代码将我的应用程序设置为默认程序。按home键转到我的应用程序

<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.DEFAULT" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
<category 安卓:name="安卓.intent.category.HOME" />

它还可以将DISABLE_KEYGUARD直接引导到我的应用程序中,而无需解锁手机

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

我试过使用System.exit(0),但它不起作用——它只是返回到我的应用程序,而不是安卓主屏幕


以下是我的代码。 它会自动返回到我的应用程序。 请告诉我密码里有什么问题

测试家庭活动。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:安卓="http://schemas.安卓.com/apk/res/安卓"
  package="com.inno.testhome"
  安卓:versionCode="1"
  安卓:versionName="1.0">
<uses-sdk 安卓:minSdkVersion="10" />

<application 安卓:icon="@drawable/icon" 安卓:label="@string/app_name" 安卓:theme="@安卓:style/Theme.Black.NoTitleBar.Fullscreen">
    <activity 安卓:name=".TesthomeActivity"
              安卓:label="@string/app_name">
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />
            <category 安卓:name="安卓.intent.category.DEFAULT" />
            <category 安卓:name="安卓.intent.category.LAUNCHER" />
            <category 安卓:name="安卓.intent.category.HOME" />
        </intent-filter>
    </activity>
    <receiver 安卓:name="StartupReceiver">
        <intent-filter>
            <action 安卓:name="安卓.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>
</manifest>

共 (2) 个答案

  1. # 1 楼答案

    读这个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(); 
    

    我想这对你有帮助

  2. # 2 楼答案

    您需要查询PackageManager以查找响应家庭意图的应用程序

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

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

    检查他的代码on github here