我想開始從我服務的方法與按一個物理按鈕(主頁,返回,菜單...) 如果用戶長按這些按鈕的之一,服務應該調用一個方法。我如何爲我的情況實現廣播接收器或監聽器?檢測在Android
3
按鈕,長按
A
回答
1
服務無法訪問UI線程。你可以做的一件事是讓活動註冊onlongclick監聽器,然後使用廣播接收器或服務的onStart(Intent intent)向服務廣播一個意圖。
0
你應該試試這個代碼:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// Perform action on click
return true;
}
});
}
}
2
不要以爲你可以自定義的Home鍵長按 - 參考增強請求:http://goo.gl/fWbYt
但是,您可以自定義一些長按這裏提到的其他硬按鈕:http://android-developers.blogspot.in/2009/12/back-and-other-hard-keys-three-stories.html
+1
,這適用於不適用於某項服務的活動 – 2012-03-02 19:03:55
相關問題
- 1. Android Fall檢測
- 2. Android:檢測USB
- 3. 在Android中檢測抖動
- 4. Android的檢測ChildClick在ExpandableListView
- 5. 如何檢測在android
- 6. 在Android中檢測Sdcard?
- 7. android在後臺檢測ActivityRecognition
- 8. Android檢測來電?
- 9. 檢測android空閒
- 10. 檢測Hough圈android
- 11. android檢測觸摸
- 12. Android手勢檢測
- 13. Android Socket-ready檢測?
- 14. Android SMS unicode檢測
- 15. android檢測觸摸
- 16. 檢測Android設備
- 17. Android雙卡檢測
- 18. Android事件檢測
- 19. 超時檢測在Android電子
- 20. Android在後臺檢測來電
- 21. 未在Eclipse中檢測到Android設備
- 22. 在Android中檢測投擲姿勢
- 23. 在android中檢測圖像的方向
- 24. 在Android上檢測wifi的IP地址?
- 25. 在Android手錶上檢測調光
- 26. 在Android Studio中未檢測到Nexus 4
- 27. 檢測打擊在Android的麥克風
- 28. 語音檢測在Android應用程序
- 29. 在Android中檢測屏幕大小
- 30. 使用openCV在android中檢測Hough線
這將適用於某項活動,而不適用於服務 – 2012-03-02 19:00:25