使用指定的意图在服务中发送广播

时间:2014-10-14 15:20:17

标签: android android-service android-broadcast

我正在尝试发送广播,但onReceive()永远不会被调用。

该服务打印以下Toast消息( When Bluetooth state is changed ) -

  

"已创建服务"

     

"服务已启动"

     

"收到BT变更!"

     

"服务自我停止"

     

"后台工作已停止"

这适用于filter1但适用于filter2

该服务仅打印以下Toast消息( When Bluetooth state is not changed ) -

  

"已创建服务"

     

"服务已启动"

DataProcessService.java -

public class DataProcessService  extends Service {
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate() 
    {               
        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

        IntentFilter filter1, filter2;

        filter1 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);

        if ((intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) 
             && !getSetting("STATUS", "").equals("Connected") && 
             !Common.isAppInForeground(getApplicationContext())) 
        {
            filter2 = new IntentFilter("true");
        }
        else
            filter2 = new IntentFilter("false");

        this.registerReceiver(mReceiver, filter1);
        this.registerReceiver(mReceiver, filter2);

        sendBroadcast(intent);

        return START_STICKY;
    }

    //The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //String action = intent.getAction();
            //BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(DataProcessService.this, "BT change received !", Toast.LENGTH_LONG).show();

            if(intent.getAction().equals("true"))
            {
                Toast.makeText(DataProcessService.this, "Service in Background", Toast.LENGTH_LONG).show();
            }

            if(intent.getAction().equals("false"))
            {
                Toast.makeText(DataProcessService.this, "Service in foreground", Toast.LENGTH_LONG).show();
            }

            if ((intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) || 
                  !getSetting("STATUS", "").equals("Connected") || Common.isAppInForeground(getApplicationContext())) {

                Toast.makeText(DataProcessService.this, "Service Self Stop", Toast.LENGTH_LONG).show();
                stopSelf();
            }
            else
            {
                Toast.makeText(DataProcessService.this, "Service Continues", Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    public void onDestroy() 
    {
        this.unregisterReceiver(mReceiver);
        Toast.makeText(this, "Background work Stopped", Toast.LENGTH_LONG).show();
    }

    public String getSetting(String key, String def) {
        SharedPreferences settings;

        try
        {
            settings = getSharedPreferences("IDDLPref", 0);

            return settings.getString(key, def);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return "";
    }
}

致电sendBroadcast(intent);的目的是致电onReceive(),但事实并非如此。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

因为filter2根据其值注册了动作“true”或“false”。 当蓝牙状态由于filter1而改变时它工作,并且当它没有改变时根本不工作,因为filter2被注册到没有发生的事情。 如果您希望它在没有更改蓝牙的情况下工作,您需要广播:

sendBroadcast(new Intent("true"))  // or "false" instead of "true", depending on the IntentFilter's value, or both if you want to make sure it will be called.