我正在尝试从服务发送广播,但它没有在接收器中收到 . 以下是我的代码
Bluetooth Service
public class BluetoothService extends Service {
public final static String TAG = "com.example.linvor.BluetoothService";
Intent intent1 = new Intent(TAG);
static boolean isRunning = false;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
android.os.Debug.waitForDebugger();
super.onStartCommand(intent, flags, startId);
//android.os.Debug.waitForDebugger();
sendBroadcast(intent1);
return START_NOT_STICKY;
}
}
我的广播接收器如下 .
ServiceBroadCastReceiver
public class ServiceBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity.myLabel.setText("BroadCAst received");
if(intent.getAction().equals(BluetoothService.TAG)) {
Log.i("Broadcast status","BroadCast received");
}
}
}
我的宣言似乎是这样的 .
manifest
<receiver
android:name=".ServiceBroadCastReceiver"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="com.example.linvor.BluetoothService"></action>
</intent-filter>
</receiver>
One more thing:
当我的服务正在运行并向我的活动发送广播(未收到)时,我的应用显示应用没有响应 .
2 回答
感谢@KeLiuyue和@Mike M.建议我的解决方案 . 但问题不在广播代码中 . 问题在于服务导致我的应用程序无法响应,因此我在我的应用程序中发送和接收广播时出现问题 . 我的服务没有回应的原因是这一行 .
我刚刚删除了这一行,一切正常 .
试试这个 .
Edited
Note
在
AndroidManifest.xml
中注册或在java代码中注册然后在java代码中发送sendBroadcast
在
onDestroy()
方法中取消注册接收器