0

我是一名Android和即時通訊的初學者,他嘗試構建一個識別未接電話的應用程序,並將該號碼存儲在遠程mysql數據庫中。該應用程序能夠識別未接來電,但網絡電話沒有發生。我無法在模擬器中運行此應用程序,因此每次使用設備時都會對此應用程序進行更改。網絡調用不是從廣播接收器中的線程發生

這是我的代碼

package com.example.missedcallverify; 

    import java.io.BufferedReader; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.util.ArrayList; 

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.NameValuePair; 
    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.entity.UrlEncodedFormEntity; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.impl.client.DefaultHttpClient; 
    import org.apache.http.message.BasicNameValuePair; 

    import com.loopj.android.http.AsyncHttpClient; 
    import com.loopj.android.http.AsyncHttpResponseHandler; 
    import com.loopj.android.http.RequestParams; 

    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.telephony.TelephonyManager; 
    import android.util.Log; 
    import android.widget.Toast; 

    public class IncommingCallReceiver extends BroadcastReceiver 
    { 

     static boolean ring=false; 
     static boolean callReceived=false; 
     static String callerPhoneNumber; 


     @Override 
     public void onReceive(final Context mContext, Intent intent) 
     { 

       // Get the current Phone State 
       String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 

       if(state==null) 
        return; 

       // If phone state "Rininging" 
       if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) 
       {   
         ring =true; 
         // Get the Caller's Phone Number 
         Bundle bundle = intent.getExtras(); 
         callerPhoneNumber= bundle.getString("incoming_number"); 
         Toast.makeText(mContext, callerPhoneNumber, Toast.LENGTH_LONG).show(); 
       } 



       // If incoming call is received 
       if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) 
       { 
         callReceived=true; 
         Toast.makeText(mContext, callerPhoneNumber+"hello ", Toast.LENGTH_LONG).show(); 
       } 


       // If phone is Idle 
       if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) 
       { 
         // If phone was ringing(ring=true) and not received(callReceived=false) , then it is a missed call 
        if(ring==true&&callReceived==false) 
        { 
          Toast.makeText(mContext, "It was A MISSED CALL from : "+callerPhoneNumber, Toast.LENGTH_LONG).show(); 
          final Intent intnt = new Intent(mContext, 
            MyService.class); 
          intnt.putExtra("callerPhoneNumber", callerPhoneNumber); 
          mContext.startService(intnt); 

        } 
      } 
     } 



    } 

MyService.java

package com.example.missedcallverify; 

import java.io.InputStream; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class MyService extends Service { 

    String callerPhoneNumber; 

    @Override 
    public void onCreate() { 
    // TODO Auto-generated method stub 
    super.onCreate(); 


    } 

    @Override 
    public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 

    return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 

    callerPhoneNumber = intent.getStringExtra(callerPhoneNumber); 
    Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show(); 
    new Thread(){ 
     @Override 
     public void run() { 
     // TODO Auto-generated method stub 
     super.run(); 
     String result = ""; 
     InputStream is = null; 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("no", callerPhoneNumber)); 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://mywebsite.in/confirm_user.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
     }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection "+e.toString()); 
     }  
     } 
    }.start(); 
    return super.onStartCommand(intent, flags, startId); 
    } 

} 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.missedcallverify" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 
    <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" > 

     <service 
      android:name=".MyService" 
      android:enabled="true" /> 

     <!-- Register the Broadcast receiver --> 
     <receiver android:name=".IncommingCallReceiver" android:enabled="true"> 
      <intent-filter> 
        <action android:name="android.intent.action.PHONE_STATE" /> 
       </intent-filter> 
      </receiver> 
    </application> 

</manifest> 

我得到的土司說是從一個未接來電:號碼。但是網絡調用沒有發生,我也無法看到日誌。我甚至看到即使是服務開始的消息。請幫助我

回答

2

BroadcastReceiver的onReceive方法僅適用於簡短的同步操作 - 一旦方法的最後一行執行,上下文就會死機。因此,您在此處開始的任何異步操作(例如後臺線程)都將立即死亡。

如果您想通過廣播執行長期操作,您應該使用服務來執行此操作。在onReceive您應該開始您的服務,並且服務將運行這些操作。

瞭解更多關於它的信息here(主要是Receiver生命週期位)。

+0

我添加了該服務。即使那樣它不起作用。我的代碼有什麼問題嗎?請檢查並幫助我。 –

+0

快速瀏覽它似乎沒問題,你需要調試和理解到底發生了什麼 - 「不起作用」對我來說有點過於通用,無法幫助你。服務是否開始?線程是否啓動?某處有例外嗎? – nitzanj

+0

該服務無法啓動。我沒有看到吐司消息,也沒有看到數據庫中更新的數字。是否調用服務也是一個不能從接收端完成的異步任務?我是否需要一項活動才能使其發揮作用? –

0

您是否要求在您的manisfest上獲得Internet許可?

+0

這是真的我dint添加它。 –

+0

@bukkehariprasad你可以標記這個答案是正確的。這可以爲其他可能認爲自己的情況與您的情況相似的人節省大量時間 –