我的应用程序我正在通过onTaskRemoved()
的{{1}}方法发送广播。但我没有收到service
中的broadcast
。这是我的代码
broadcastReciever
我正在接收除 @Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.e("TaskRemoved","TaskRemoved");
commonSessionManager.setConnected(false);
broadCastUpdate(GymMainActivity.APP_REMOVED_FROM_RECENTS);
}
public void broadCastUpdate(String action){
Intent intent=new Intent();
intent.putExtra(GymMainActivity.APP_REMOVED_FROM_RECENTS,action);
sendBroadcast(intent);
}
private BroadcastReceiver mGattUpdateReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (WRONG_DATA.equalsIgnoreCase(action)) {
Toast.makeText(GymMainActivity.this, "Please measure your vitals again", Toast.LENGTH_SHORT).show();
isFalseDataSent = true;
}
if (ACTION_GATT_CONNECTED.equalsIgnoreCase(action)) {
Toast.makeText(GymMainActivity.this, "connected", Toast.LENGTH_SHORT).show();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isFinishing()) {
ShowConnectDialog();
}
}
});
} else if (ACTION_GATT_DISCONNECTED.equalsIgnoreCase(action)) {
Toast.makeText(GymMainActivity.this, "disconnected", Toast.LENGTH_SHORT).show();
} else if (ACTION_DATA_AVAILABLE.equalsIgnoreCase(action)) {
connectDialog.dismiss();
String[] result = intent.getStringArrayExtra(EXTRA_DATA);
for (int i = 0; i < result.length; i++) {
Log.e("result " + i, result[i]);
}
Log.e("isDisplayed", isDisplayed + "");
if (!isDisplayed) {
if (isFalseDataSent) {
mBluetoothController.powerOff();
isFalseDataSent = false;
}
commonSessionManager.setConnected(true);
Intent vitalDataIntent = new Intent(GymMainActivity.this, GymVitalDisplayActivity.class);
vitalDataIntent.putExtra("userDataBundle",userDataBundle());
vitalDataIntent.putExtra("vitalData", result);
startActivity(vitalDataIntent);
finish();
isDisplayed = true;
}
}
if(action.equalsIgnoreCase(APP_REMOVED_FROM_RECENTS)){
Toast.makeText(GymMainActivity.this,"APP_REMOVED_FROM_RECENTS",Toast.LENGTH_SHORT).show();
btnScan.setVisibility(View.VISIBLE);
if(btnConnect.getVisibility()==View.VISIBLE){
btnConnect.setVisibility(View.GONE);
}
if(txtConnected.getVisibility()==View.VISIBLE){
txtConnected.setVisibility(View.GONE);
}
}
}
};
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_GATT_CONNECTED);
intentFilter.addAction(ACTION_GATT_DISCONNECTED);
intentFilter.addAction(ACTION_DATA_AVAILABLE);
intentFilter.addAction(WRONG_DATA);
intentFilter.addAction(APP_REMOVED_FROM_RECENTS);
return intentFilter;
}
public final static String ACTION_GATT_CONNECTED =
"com.example.demo.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED =
"com.example.demo.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_DATA_AVAILABLE =
"com.example.demo.le.ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA =
"com.example.demo.le.EXTRA_DATA";
public final static String WRONG_DATA =
"com.example.demo.le.WRONG_DATA";
public final static String APP_REMOVED_FROM_RECENTS="com.example.demo.le.APP_REMOVED_FROM_RECENTS";
答案 0 :(得分:1)
可能是因为你没有在意图上设置动作,而是作为额外的动作。将您的代码更改为该代码并查看其是否有效:
public void broadCastUpdate(String action){
Intent intent=new Intent(action);
sendBroadcast(intent);
}