显示通知时,蓝牙断开 - Android电子 [英] Displaying a notification when bluetooth is disconnected - Android

查看:132
本文介绍了显示通知时,蓝牙断开 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,将显示一个的通知用户如果蓝牙设备突然屏幕上出现超出范围从我Android设备。目前,我有以下code,但不显示任何通知。

我在想,如果有可能我不应该使用 ACTION_ACL_DISCONNECTED ,因为我相信蓝牙堆栈会预计该状态的数据包断开连接请求。我的要求规定,蓝牙设备将断开没有警告。

感谢您的任何援助!

BluetoothNotification.java:
//这是创建通知的地方。

 进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
公共类BluetoothNotification扩展活动
{
公共静态最终诠释NOTIFICATION_ID = 1;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    / **定义配置我们的通知* /
    INT图标= R.drawable.logo;
    CharSequence的tickerText =这是一个简单的通知;
    时长= System.currentTimeMillis的();
    上下文的背景下= getApplicationContext();
    CharSequence的contentTitle =样品的通知;
    的CharSequence contentText =此通知已作为BT断开的结果而产生;
    意图notificationIntent =新意图(这一点,BluetoothNotification.class);
    的PendingIntent contentIntent = PendingIntent.getActivity(在此,0,notificationIntent,0);    / **采用上述结构初始化通知* /
    最后通知的通知=新的通知(图标,tickerText时);
    notification.setLatestEventInfo(背景下,contentTitle,contentText,contentIntent);    / **从NotificationManager检索参考* /    字符串NS = Context.NOTIFICATION_SERVICE;
    最后NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);
    mNotificationManager.notify(NOTIFICATION_ID,通知);    完();
}
}

从片段OnCreate中://位于Controls.java

 的IntentFilter滤波器1 =新的IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver,过滤器1);

从片段Controls.java:

 私人最终的BroadcastReceiver mReceiver =新的广播接收器(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        字符串行动= intent.getAction();
        BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);        如果(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(动作)){
               //设备已断开连接
            NotificationManager纳米=(NotificationManager)
                    getSystemService(NOTIFICATION_SERVICE);
        }    }};


解决方案

什么是你的广播接收器,你的活动之间的联系?

有没有必要这个活动,把一切都在你的广播接收器显示的通知。

I am trying to create a program that will display a notification to the user if a Blue tooth device suddenly comes out of range from my Android device. I currently have the following code but no notification is displayed.

I was wondering if it was possible I shouldn't use ACTION_ACL_DISCONNECTED because I believe the bluetooth stack would be expecting packets that state a disconnect is requested. My requirements state that the bluetooth device will disconnect without warning.

Thank you for any assistance!

BluetoothNotification.java: //This is where the notification is created.

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();
}    
}

Snippet from OnCreate: //Located in Controls.java

IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);

Snippet from 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);
        }

    }

};

解决方案

What is the link between your BroadCastReceiver and your Activity ?

There is no need for this activity, put everything in your broadcast receiver to display the notification.

这篇关于显示通知时,蓝牙断开 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆