Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

android - onReceive method doesn't get called

public class BroadcastTest extends Activity {
BroadcastReceiver receiver;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try{
    receiver=new Receiver(this);
    registerReceiver(receiver,new IntentFilter(Intent.ACTION_CALL_BUTTON));
    }catch(Exception e){
        Log.d("error",e.getMessage());
    }
  }
}

and another class

public class Receiver extends BroadcastReceiver{
public Receiver(BroadcastTest broadcastTest) {
    // TODO Auto-generated constructor stub
}

@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    Log.d("Fired","Hi");
}
}

is onReceive method of receiver class supposed to be called when i press call button?if yes then it is not geting call.What i am doing wrong here.I am not seeing anything in logcat while pressing call button.Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This Blog post describes some common pitfalls with AlarmManager and BroadcastReceiver. Examples included! Hope that helps.

EDIT: Some common pitfalls:

  1. Receiver not declared in AndroidManifest.xml

    Declare the receiver in the Manifest-file:

    <receiver android:name="net.fusonic.testapp.receivers.TestAlarmReceiver"></receiver>
    
  2. Receiver in the Manifest xml is misspelled

    Always remember that the whole Android-System is case sensitive. So check your spelling is correct in the AndroidMainfest.xml. Remember that the eclipse refactoring functions do not change packagename correctly if you use the short form like “.receivers.TestAlarmReceiver“.

  3. PendingIntent requestCode missing?

    If you create a PendingIntent for your Receiver, please add a requestCode – even it is a random number! Without your “onReceive” code never get called!

  4. AVD running for a long time (very tricky)

    Be aware of using the AVDs especially if your working with “REALTIME_WAKEUP” and SystemClock… So if you try to test your alarm, please restart the AVD or test on a real device!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...