简体   繁体   English

使用1次的BroadcastReceiver

[英]BroadcastReceiver using 1 time

Good evening, I have a simple program that activates the BroadcastReceiver to detect the number when I receive a call, the problem is that when I get the call, will write again in my database, and when I disconnect the call records again, summing writes 3 ​​times! 晚上好,我有一个简单的程序,当我接到电话时,它会激活BroadcastReceiver来检测电话号码,问题是当我接到电话时,它将再次写入数据库,而当我再次断开呼叫记录时,将写入总和3次! and I only want 1, when it is playing! 而且我只想在玩的时候1! Another problem is when the phone is ringing, writes logo in the database, (here is okay!) But it does not answer the call, it writes the call again! 另一个问题是电话响起时,在数据库中写入徽标(这里可以!),但是它不接听电话,而是再次写入电话! the code is this: 代码是这样的:

my broadcast: its 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");     
            try {
                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);
        return 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 also includes the call state (idle, ringing, offhook). 广播意图还包括呼叫状态(空闲,响铃,摘机)。 You can read it with the key Telephony.EXTRA_STATE , then compare it with the possible values. 您可以使用Telephony.EXTRA_STATE键读取它,然后将其与可能的值进行比较。 For example: 例如:

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
{
    String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    // call your asynctask ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM