[英]android: receiver not receiving any broadcast when bluetooth device disconnected
[英]Displaying a notification when bluetooth is disconnected - Android
我正在嘗試創建一個程序,如果藍牙設備突然超出了我的Android設備的范圍,它將向用戶顯示notification
。 我目前有以下代碼,但沒有顯示通知。
我想知道是否有可能不應該使用ACTION_ACL_DISCONNECTED
因為我相信bluetooth
堆棧會期望請求斷開連接的數據包。 我的要求是,藍牙設備將在沒有警告的情況下斷開連接。
感謝您的協助!
BluetoothNotification.java://這是創建通知的位置。
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class BluetoothNotification extends Activity
{
public static final int NOTIFICATION_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Define configuration for our notification */
int icon = R.drawable.logo;
CharSequence tickerText = "This is a sample notification";
long when = System.currentTimeMillis();
Context context = getApplicationContext();
CharSequence contentTitle = "Sample notification";
CharSequence contentText = "This notification has been generated as a result of BT Disconnecting";
Intent notificationIntent = new Intent(this, BluetoothNotification.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
/** Initialize the Notification using the above configuration */
final Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
/** Retrieve reference from NotificationManager */
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(NOTIFICATION_ID, notification);
finish();
}
}
OnCreate的代碼段://位於Controls.java中
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
來自Controls.java的代碼段:
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_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
}
}
};
您的BroadCastReceiver和您的活動之間有什么聯系?
無需執行此活動,請將所有內容放在廣播接收器中以顯示通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.