java - BroadcastReceiver using 1 time -
good evening, have simple programme activates broadcastreceiver observe number when receive call, problem when call, write 1 time again in database, , when disconnect phone call records again, summing writes 3 times! , want 1, when playing! problem when phone ringing, writes logo in database, (here okay!) not reply call, writes phone call again! code this:
my broadcast: works!
public class mybroadcastreceiver extends broadcastreceiver { string idtelemovel="1"; string phone_number; @override public void onreceive(context context, intent intent) { system.out.println("entrou no broadcastreceiver!!! : "); bundle bundle = intent.getextras(); phone_number = bundle.getstring("incoming_number"); system.out.println("phone number : " + phone_number); log.i("zz", "phone number : " + phone_number); new summaryasynctask().execute((void) null); } class summaryasynctask extends asynctask<void, void, boolean> { private void postdata(string phone_number) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.aaa.com/insert.php"); seek { arraylist<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(4); namevaluepairs.add(new basicnamevaluepair("street", phone_number)); namevaluepairs.add(new basicnamevaluepair("house", idtelemovel)); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); } catch(exception e) { log.e("log_tag", "error: "+e.tostring()); system.out.print("*********fail*********"); } } @override protected boolean doinbackground(void... params) { postdata(phone_number); homecoming null; } } } manifest:
<uses-sdk android:minsdkversion="18" android:targetsdkversion="19" /> <uses-permission android:name="android.permission.write_contacts" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <receiver android:name=".mybroadcastreceiver" > <intent-filter> <action android:name="android.intent.action.phone_state" /> </intent-filter> </receiver> <activity android:name="com.example.a.broadcastreceiver" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity><service android:name=".myservices" /> </application> </manifest>
the broadcast intent includes phone call state (idle, ringing, offhook). can read key telephony.extra_state, compare possible values. example:
string state = intent.getstringextra(telephonymanager.extra_state); if (telephonymanager.extra_state_ringing.equals(state)) { string phonenumber = intent.getstringextra(telephonymanager.extra_incoming_number); // phone call asynctask ... } java android mysql
No comments:
Post a Comment