我想确切知道设备主页按钮的操作是什么?即当您点击主页按钮时会发出什么意图,意图类别和行动?这会带回到空白的主屏幕。我想知道在单击我自己的自定义按钮时实现此操作所涉及的内容。谢谢(PS我知道它不是标准的,但我的设备也不是。)
答案 0 :(得分:13)
如果要显示主屏幕,可以通过以下方式执行:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
更新:查看此示例应用:http://developer.android.com/resources/samples/Home/index.html
答案 1 :(得分:0)
这是xml中的意图,如果您正在寻找它:
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- The following two intent-filters are the key to set homescreen -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 2 :(得分:0)
答案 3 :(得分:-1)
您可以参考代码:
Button btnHome;
btnHome = (Button) findViewById(R.id.Home);
btnHome.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// sendKey(KeyEvent.KEYCODE_HOME);
myHandler.sendEmptyMessage(Home);
}
});
class myRunnable implements Runnable {
public myRunnable(int key) {
this.keycode = key;
}
int keycode;
@Override
public void run() {
sendKey(keycode);
}
public void sendKey(int keyCode) {
System.out.println("Judy--------------------->sendkey " + keyCode);
long now = SystemClock.uptimeMillis();
long n = System.currentTimeMillis();
try {
KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
keyCode, 0);
KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP,keyCode, 0);
IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager
.getService("window"));
wm.injectKeyEvent(down, false);
wm.injectKeyEvent(up, false);
} catch (RemoteException e) {}
}
}