【问题标题】:Go back to launcher android programmatically以编程方式返回启动器 android
【发布时间】:2012-06-18 03:39:34
【问题描述】:

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

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>

【问题讨论】:

  • 我相信添加finish();将自动带您回到您启动应用程序的位置。如果您有多项活动,请尝试完成所有活动,以便让您回到开始的地方。
  • 第二个答案是我的代码。有什么问题可以告诉我吗?

标签: java android android-intent


【解决方案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(); 

我想这会对你有所帮助

【讨论】:

  • 它也会自动回到我的APP...不过谢谢你的回答
【解决方案2】:

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

这就是您无法通过退出应用程序返回默认启动器的原因。 达到您想要的结果的唯一方法是change the default launcher

【讨论】:

    【解决方案3】:

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

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

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

    检查他的代码on github here

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2012-09-27
      • 2018-10-07
      • 2012-09-22
      相关资源
      最近更新 更多