6

我想通过两个按钮启动和停止广播接收器。我已经在两个按钮侦听器中为此编写了代码。

代码如下:-

在 startButton 中,我编写了注册接收器的代码,如下所示

     startCallBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            startCallBtn.setVisibility(View.INVISIBLE);
            stopCallBtn.setVisibility(View.VISIBLE);


            //register call receiver.
            registerReceiver(callReceiver, new IntentFilter(
            "android.intent.action.CALL"));
           System.out.println("Registered Call Receiver Perfectly");
        }
    });

在停止按钮中,我将停止接收器的调用写为

   stopCallBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            stopCallBtn.setVisibility(View.INVISIBLE);
            startCallBtn.setVisibility(View.VISIBLE);
            stopService(new Intent(MainActivity.this, SpeakOut.class));
            FileOutputStream fos;

          //Unregister sms receivers
            unregisterReceiver(callReceiver);
            System.out.println("Unregistered Call Broadcast Receiver");
        }
    });

接收新呼叫时,侦听器仍在呼叫

4

3 回答 3

12

你可以试试这个代码enabledisable BroadcastReceiver..我希望这对你有帮助..

public void enableBroadcastReceiver(View view)
{

    ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
    PackageManager pm = this.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
   }
   /**
    * This method disables the Broadcast receiver registered in the AndroidManifest file.
    * @param view
    */
   public void disableBroadcastReceiver(View view){
    ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
    PackageManager pm = this.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
    Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
   }   
于 2013-02-05T06:41:14.820 回答
3

清单中

 <receiver android:name="your receiver name">

主要 活动

BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.w("Check", "Inside On Receiver");
            Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BROADCAST_ACTION);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        registerReceiver(br, filter);

    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(br);
    }
}
于 2013-02-05T06:36:12.703 回答
1

确保您调用的对象与您调用的对象unregisterReceiver()相同。如果您在释放之前为同一个接收器创建新对象,则已注册的旧接收器对象不能再取消注册,因为您已为其分配了新的内存块。receiverregisterReceiver()

例如,这样做:

MyBroadcastReceiver receiver = new MyBroadCastReceiver();
registerReceiver(receiver);
unregisterReceiver(receiver);

receiver = new MyBraodCastReceiver();
于 2013-02-05T06:44:08.070 回答