Android 개발을 처음 사용합니다. 나는 블루투스 이벤트 수신을위한 브로드 캐스트 리시버를 선언하는 서비스로 테스트 응용 프로그램을 정의 - 내 활동에서 서비스를 시작 Android : 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);
}
}
}
로 내 브로드 캐스트 리시버가 정의
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 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>
내가 잘못 뭐하는 거지 디버그를 도와주세요 매니페스트 파일에 설정된 권한을 가지고 않습니다.
아니요,이 변경을했지만 여전히 브로드 캐스트 수신기가 호출되지 않았습니다. – user1608065