我正在尝试从广播中启动服务,但由于某种原因,我的服务永远不会被调用。这是我的接收器:
private void initReceiver() {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("telecom.IT.DigiCARE.REC")) {
Log.d("MainActivity", "Intent received from server!");
byte[] data = intent.getByteArrayExtra("Data");
String params = intent.getStringExtra("Params");
Intent i = new Intent(MainActivity.this, DecodeService.class);
i.putExtra("Data", data);
i.putExtra("Params", params);
startService(i);
}
}
};
registerReceiver(receiver, new IntentFilter("telecom.IT.DigiCARE.REC"));
}
这是我的服务:
public class DecodeService extends Service {
@Override
public void onStart(Intent intent, int startId) {
String params = intent.getStringExtra("Params");
byte[] data = intent.getByteArrayExtra("Data");
decodeMessage(params, data);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String params = intent.getStringExtra("Params");
byte[] data = intent.getByteArrayExtra("Data");
decodeMessage(params, data);
return START_STICKY;
}
}
我期待错误出现在清单中的某个地方,但我没有看到它。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.telecomIT.digicare"
android:versionCode="1"
android:versionName="1.0" >
<services
android:name=".DecodeService"
android:enabled="true" />
这不是因为广播不起作用,因为 startService 被调用,但是没有任何反应..