2011-04-01 199 views
0

我正在嘗試在我正在玩的小型Android應用程序中註冊broadcastreceiver。我認爲我正在做我應該做的事,但接收者仍然沒有註冊。它一定是我錯過的小事,它讓我發瘋。Android廣播接收器註冊問題

public class CreateReceiver extends Activity 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    }//end of onCreate 

    BroadcastReceiver myreceiver = new BroadcastReceiver() 
    { 
     @Override 
     public void onReceive(Context recvc, Intent recvi) 
     { 

     } 
    }; 

    @Override 
    public void onResume() 
    { 
     super.onResume(); 

     IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON); 

     if (this.registerReceiver(myreceiver, intentFilter) == null) 
     { 
      Toast.makeText(this, "Could not register receiver", Toast.LENGTH_LONG).show(); 
     }//end of if 
    }//end of onResume 

    @Override 
public void onPause() 
{ 
    this.unregisterReceiver(myreceiver); 
    super.onPause(); 
}//end of onPause 
}//end of CreateReceiver 

誰能告訴我我做錯了什麼?謝謝。

回答

0

我認爲這個link會幫助你。您也可以從here

0

得到參考你必須從註冊您的廣播接收器內onCreate

public class CreateReceiver extends Activity 
{ 
    private IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON); 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     registerReceiver(myreceiver , intentFilter); 
    }//end of onCreate 

    BroadcastReceiver myreceiver = new BroadcastReceiver() 
    { 
     @Override 
     public void onReceive(Context recvc, Intent recvi) 
     { 

     } 
    }; 

    ...