6

2つのボタンからブロードキャストレシーバーを開始および停止したい。このコードは 2 つのボタン リスナーで記述しました。

コードは次のとおりです。

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

このコードを試すことができenableますdisable 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

Make sure that you are calling unregisterReceiver() of same receiver object on which you have called registerReceiver(). If you are creating new object for same receiver before releasing it, then, the old receiver object which is registered can't be unregistered anymore as you have allocated new memory block to it.

For example, do this:

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

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