我想创建一个程序,将显示一个的通知
用户如果蓝牙设备突然屏幕上出现超出范围从我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.
上一篇:使用C ++ DLL从C#DLL
下一篇:< META NAME ="视" ...标签不正确地在Android(三星Galaxy Tab 2)方向改变后工作正确地、方向、标签、工作