android - Receiving the Broadcast -
i want make application gets incoming calling number if call specific number stuff . want incoming calling number when application not running .. using braodcastreceiver incoming number .
i have 2 java class 1 extends activity , other extends braodcastreceiver getting incoming calling number .
main class extends activity :
package digicare.ringmanager; import android.os.bundle; import android.app.activity; import android.view.menu; public class main_activity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_layout); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main_layout, menu); return true; } }
it pretty simple , number checker class extends broadcastreceiver :
package digicare.ringmanager; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.media.audiomanager; import android.os.bundle; import android.telephony.telephonymanager; import android.util.log; public class number_checker extends broadcastreceiver { private int ringer_mode ; private string am_str; @override public void onreceive(context context, intent intent) { // todo auto-generated method stub audiomanager =(audiomanager)context.getsystemservice(context.audio_service); log.w("start", "starting main_activity"); am_str=string.valueof(ringer_mode); log.w("ringer_mode @ start", am_str); //setting ringer mode on number match try { bundle extras=intent.getextras(); if (extras !=null){ string state = extras.getstring(telephonymanager.extra_state); log.w("state @ start",state); if (state.equals(telephonymanager.extra_state_ringing)){ string phonenumber = extras.getstring(telephonymanager.extra_incoming_number); //am.setringermode(1); ringer_mode =am.getringermode(); am_str=string.valueof(ringer_mode); log.w("ringer_mode @ ringing", am_str); log.w("number", phonenumber); if (phonenumber.equals("1234")){ log.w("yahoo", "number matched"); if (ringer_mode==audiomanager.ringer_mode_silent || ringer_mode==audiomanager.ringer_mode_vibrate ){ am.setringermode(audiomanager.ringer_mode_normal); log.w("phone number matched .!", "now , ringer mode normal"); int now_nor =am.getringermode(); string now_nor_str=string.valueof(now_nor); log.w("ring_mode @ num matched",now_nor_str); } } } if (state.equals(telephonymanager.extra_state_offhook ) || state.equals(telephonymanager.extra_state_idle)){ int now_nor =am.getringermode(); string now_nor_str=string.valueof(now_nor); log.w("ring_mode @ offhock",now_nor_str); if (ringer_mode==audiomanager.ringer_mode_normal){ am.setringermode(audiomanager.ringer_mode_normal ); log.w("normal", ""); }else if (ringer_mode==audiomanager.ringer_mode_silent ){ am.setringermode(audiomanager.ringer_mode_silent ); log.w("silent", ""); }else if (ringer_mode==audiomanager.ringer_mode_vibrate ){ am.setringermode(audiomanager.ringer_mode_vibrate); log.w("vibrat", ""); } // log.w("again", "now ringer mode "); int =am.getringermode(); string now_str=string.valueof(now); log.w("ring_mode @ end ",now_str); } } } catch (exception e) { // todo: handle exception log.w("my_debug_tag", e); } } }
and androidmanifist.xml :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="digicare.ringmanager" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="5" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.read_phone_state"/> <uses-permission android:name="android.permission.modify_audio_settings"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="digicare.ringmanager.main_activity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name="number_checker" > <intent-filter> <action android:name="android.intent.action.phone_state"/> </intent-filter> </receiver> </application> </manifest>
but not working cannot incoming calling number . wot ???? have call number_checker class register braodcast ???
please new android developer
you can either dynamically register instance of class context.registerreceiver()
or statically publish implementation through tag in androidmanifest.xml
.
note: if registering receiver in activity.onresume()
implementation, should unregister in activity.onpause()
. (you won't receive intents when paused, , cut down on unnecessary system overhead). not unregister in activity.onsaveinstancestate()
, because won't called if user moves in history stack.
also check link :
http://www.vogella.com/articles/androidbroadcastreceiver/article.html
new update:
update 2
put .
before receiver name. <receiver android:name=".number_checker" >
Comments
Post a Comment