android - How to send the incoming phone call number to another activity using intent -
i want display phone number textview masking last 4 digits if possible, can't run properly, application stops... here sample/reference code of want do. if using toast displays number when using intent code not work: runtime error.
sender(2nd activity)
import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.os.bundle; import android.telephony.telephonymanager; import android.widget.toast; public class mycallreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub if (intent.getstringextra(telephonymanager.extra_state).equals(telephonymanager.extra_state_ringing)) { string incomingnumber = intent.getstringextra(telephonymanager.extra_incoming_number); intent = new intent(context,mainactivity.class); a.putextra("key", incomingnumber); context.startactivity(a); }
main activity
import android.os.bundle; import android.app.activity; import android.widget.textview; public class mainactivity extends activity { textview tv1,tv2; string gotbread; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initialize(); tv1.setselected(true); bundle gotbasket = getintent().getextras(); gotbread = gotbasket.getstring("key"); tv2.settext(gotbread); } public void initialize() { // todo auto-generated method stub tv1 = (textview) this.findviewbyid(r.id.tv1); tv2 = (textview) this.findviewbyid(r.id.tv2); } }
here logcat hope helps...
08-18 02:50:10.121: d/androidruntime(1306): shutting down vm 08-18 02:50:10.121: w/dalvikvm(1306): threadid=1: thread exiting uncaught exception (group=0xb2a3fba8) 08-18 02:50:10.131: e/androidruntime(1306): fatal exception: main 08-18 02:50:10.131: e/androidruntime(1306): process: com.example.detectincomingcall, pid: 1306 08-18 02:50:10.131: e/androidruntime(1306): java.lang.runtimeexception: unable start activity componentinfo{com.example.detectincomingcall/com.example.detectincomingcall.mainactivity}: java.lang.nullpointerexception 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activitythread.access$800(activitythread.java:135) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 08-18 02:50:10.131: e/androidruntime(1306): @ android.os.handler.dispatchmessage(handler.java:102) 08-18 02:50:10.131: e/androidruntime(1306): @ android.os.looper.loop(looper.java:136) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activitythread.main(activitythread.java:5017) 08-18 02:50:10.131: e/androidruntime(1306): @ java.lang.reflect.method.invokenative(native method) 08-18 02:50:10.131: e/androidruntime(1306): @ java.lang.reflect.method.invoke(method.java:515) 08-18 02:50:10.131: e/androidruntime(1306): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 08-18 02:50:10.131: e/androidruntime(1306): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 08-18 02:50:10.131: e/androidruntime(1306): @ dalvik.system.nativestart.main(native method) 08-18 02:50:10.131: e/androidruntime(1306): caused by: java.lang.nullpointerexception 08-18 02:50:10.131: e/androidruntime(1306): @ com.example.detectincomingcall.mainactivity.oncreate(mainactivity.java:21) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activity.performcreate(activity.java:5231) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-18 02:50:10.131: e/androidruntime(1306): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 08-18 02:50:10.131: e/androidruntime(1306): ... 11 more
turn evaluation around , use yoda notation. ensure not npe trying execute null.equals()
. sure check argument not null. java has strict evaluation order left right can use short-circuit evaluation.
if (null != intent && telephonymanager.extra_state_ringing.equals(intent.getstringextra(telephonymanager.extra_state)))
and in oncreate() might consider being bit paranoid
intent intent = getintent(); if (null != intent) { bundle gotbasket = intent.getextras(); if (null != gotbasket) { gotbread = gotbasket.getstring("key"); tv2.settext(gotbread); } }
Comments
Post a Comment