1

我正在尝试创建一个程序,如果蓝牙设备突然超出我的 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);
        }

    }

};
4

1 回答 1

0

您的 BroadCastReceiver 和您的 Activity 之间的联系是什么?

不需要此活动,将所有内容放在广播接收器中以显示通知。

于 2012-12-06T05:01:02.187 回答