我是Android开发的新手。我已经定义了一个带有服务的测试应用程序,我在其中声明了一个BroadcastReceiver来接收蓝牙事件 -
public class MyService extends Service
{
BluetoothEventsReceiver mBluetoothEventsReceiver = null;
@Override
public int onStartCommand(Intent i, int flags, int startId) {
// register the receiver to listen for Bluetooth Connected/Dis-connected events
if (mBluetoothEventsReceiver != null) {
mBluetoothEventsReceiver = new BluetoothEventsReceiver();
Log.e(TAG, "Register receiver=" + mBluetoothEventsReceiver);
IntentFilter intent = new IntentFilter("android.bluetooth.device.action.ACL_CONNECTED");
intent.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
getApplicationContext().registerReceiver(mBluetoothEventsReceiver, intent);
}
return super.onStartCommand(i, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
我的BroadcastReceiver定义为
public class BluetoothEventsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Log.e(TAG, "Received Event" + " ACTION_ACL_DISCONNECTED" );
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(TAG, "device=" + device);
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Log.e(TAG, "Received Event" + " ACTION_ACL_CONNECTED" );
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(TAG, "device=" + device);
}
}
}
我从我的Activity启动服务,我希望BroadcastReceiver在连接和断开蓝牙耳机时打印日志消息,但不打印日志。所以,我猜它没有被称为。
public class MyActivity extends Activity {
// Debugging
public final static String TAG ="MyActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(TAG, "Starting service");
startService(new Intent(".MyService"));
}
此外,当我将接收器添加到Manifest文件时,然后调用BluetoothEventsReceiver。但根据我的理解,我不需要在Manifest文件中声明接收器,如果我希望接收器仅在服务运行时才处于活动状态。
我确实在清单文件中设置了权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="@string/app_name">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".MyService" android:process=":remote">
<intent-filter>
<!-- These are the interfaces supported by the service, which
you can bind to. -->
<action android:name=".MyService" />
</intent-filter>
</service>
</application>
</manifest>
请帮助调试我做错了什么。
答案 0 :(得分:0)
更改您的意图过滤器:
IntentFilter intent = new IntentFilter("android.bluetooth.device.action.ACL_CONNECTED");
intent.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
到此:
IntentFilter intent = new IntentFilter();
intent.addAction("android.bluetooth.device.action.ACL_CONNECTED");
intent.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
您创建的过滤器似乎与蓝牙意图不匹配,因为它们确实附加了额外的数据,而您的过滤器仅适用于只有一个操作的空意图。
以下是API的javadoc:
public IntentFilter(String action) 自:API级别1 新的IntentFilter匹配单个操作而没有数据。如果随后未指定数据特征,则过滤器将仅匹配不包含数据的意图。
答案 1 :(得分:0)
此startService(new Intent(".MyService"))
无效。您需要提供Service类名称的实际路径。相对路径仅在AndroidManifest文件中使用,不应在代码中使用。
在您的活动中尝试以下内容:
Intent intent = new Intent(this, MyService.class);
this.startService(intent);
其中 MyService 是指服务的实际完全限定名称。