IM在我的最後一個項目的工作爲我的拼貼和IM卡住尋找一個android.i藍牙通知代碼藍牙通知代碼所需要的代碼來通知我,每當我的藍牙打開或關閉爲Android
Q
爲Android
1
A
回答
1
您需要在您的代碼中添加廣播接收器,以便在您的活動課程中開啓或關閉藍牙。供大家參考:
public void onCreate() {
...
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
this.registerReceiver(mReceiver, filter3);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
... //Device found
}
else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
... //Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
... //Done searching
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
... //Device is about to disconnect
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
... //Device has disconnected
}
}
+0
感謝您的代碼,但我需要它做的是在通知欄中添加一條聲明,說藍牙處於開啓或關閉狀態或正在使用中 – 2012-07-06 06:34:05
+0
確定那麼一旦您知道了是它打開或關閉,你可以添加通知狀態..對嗎?或者你不知道如何添加通知欄本身? – 2012-07-06 06:36:48
+0
實際上,我們正在接受第四年的基礎知識教育,現在我們基本上必須依靠網絡獲取代碼和信息 – 2012-07-06 07:10:05
2
OK then do it like that when you bluetooth is on or off
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
int icon = R.drawable.nishansahib1; // icon from resources
CharSequence tickerText = "SatShreeAkal"; // ticker-text
long when = System.currentTimeMillis(); // notification time
//Context context = getApplicationContext(); // application Context
CharSequence contentTitle = ""; // message title
//
CharSequence contentText= "YPUR BLUETOOTH IS ON OR OFF"; // message text
final int NOTIF_ID = 1234;
Intent notificationIntent = new Intent(context, your classname);//if u want to call a class
notificationIntent.putExtra("DISPLAY",contentText);//if u want to pass intent
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notofManager.notify(NOTIF_ID,notification);
}
相關問題
- 1. 爲Android
- 2. 爲Android
- 3. 爲Android
- 4. 爲Android
- 5. 爲Android
- 6. 爲Android
- 7. 爲Android
- 8. 爲Android
- 9. 爲Android
- 10. 爲Android
- 11. 爲Android
- 12. 爲Android
- 13. 爲Android
- 14. 爲Android
- 15. 爲Android
- 16. 爲Android
- 17. 爲Xamarin的Android
- 18. curl庫爲Android
- 19. 編碼爲Android
- 20. 的Jython爲Android
- 21. Android AccesToken爲空
- 22. 爲Android解析
- 23. parse.com爲Android
- 24. ImageResizer庫爲Android
- 25. Android DialogFragment爲空
- 26. 爲Android代碼
- 27. SLF4J爲Android
- 28. 爲Android API 21
- 29. orientationchange JQM - 爲Android
- 30. Android ListView爲空
沒有ü嘗試做一個谷歌搜索呢? – 2012-07-06 06:14:28
是啊我google了但沒有出現 – 2012-07-06 06:16:39