2013-02-05 215 views
5

我想從兩個按鈕啓動和停止廣播接收器。 我已經在兩個按鈕監聽器中編寫了代碼。啓動和停止廣播接收器

的代碼如下: -

在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"); 
     } 
    }); 

收聽新電話時仍有收聽者打電話

+0

你把這個接收器還的Manifest.xml? –

回答

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); 
    } 
} 
10

你可以試試這個代碼enabledisableBroadcastReceiver ..我希望這將幫助你..

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(); 
    } 
1

製作確定你打電話給unregisterReceiver()receiver您稱爲registerReceiver()的對象。如果您在釋放它之前爲同一個接收器創建新對象,那麼已註冊的舊接收器對象不能再註銷,因爲您已爲其分配新的內存塊。

例如,做到這一點:

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

receiver = new MyBraodCastReceiver();