2017-02-22 110 views
0

我想在android中獲取被叫號碼,但是當我開始撥出電話時,它失敗我正在使用廣播接收器並將其註冊到服務中以保持偵聽,如果活動不在焦點中是我的代碼。在廣播接收器中接收呼叫意圖

Menifest.xml

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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" > 
    <activity android:name=".MainActivity" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
      <action android:name="android.intent.action.BOOT_COMPLETED"> 
       <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 


      </action> 
     </intent-filter> 
    </activity> 

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


    <receiver 
     android:name="com.example.vampirepc.androidservice.OutgoingReceiver" 
     android:enabled="true" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
     </intent-filter> 
    </receiver> 

</application> 
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
</manifest> 

廣播接收器類

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

    Toast.makeText(ctx, 
      "Inside broadcast", 
      Toast.LENGTH_LONG).show(); 


    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

    Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show(); 


} 

服務

@Override 
public void onCreate() { 
    Toast.makeText(getApplicationContext(),"Service created",Toast.LENGTH_SHORT).show(); 

    try { 


     IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"); 

     OutgoingReceiver myReceiver = new OutgoingReceiver(); 
     registerReceiver(myReceiver, filter); 


    } catch (Exception e) { 

     Toast.makeText(getApplicationContext(),"Exception is "+String.valueOf(e),Toast.LENGTH_SHORT).show(); 

    } 



} 
+1

在你的情況下,沒有必要在運行時註冊接收器,只需在清單中定義它。 –

+0

當我撥號時仍然不工作應用程序停止工作 – DumpsterDiver

+1

請發佈堆棧跟蹤。 –

回答

0

請添加權限在上面,幫我

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

    android:name="android.permission.PROCESS_OUTGOING_CALLS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
       <action android:name="android.intent.action.BOOT_COMPLETED"> 
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 


       </action> 
      </intent-filter> 
     </activity> 
     <receiver 
      android:name="com.example.vampirepc.androidservice.OutgoingReceiver" 
      android:enabled="true" 
      android:exported="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
      </intent-filter> 
     </receiver> 

    <service android:name=".MyService" /> 
    </application> 
    <uses-permission 
    </manifest> 
+0

請參閱此鏈接https://www.simplifiedcoding.net/android-studio-tutorial-an-app-to-track-outgoing-calls/ –

0

你在這一行實現:

Toast.makeText(ctx, 
     "Inside broadcast", 
     Toast.LENGTH_LONG).show(); 

您用來代替背景CTX。

0

謝謝所有我已經解決了我自我的問題我在烤麪包機上使用了錯誤的上下文正確的onReceive是。 我正在使用初始化的ctx。

Context ctx; 


    public void onReceive(Context context, Intent intent) { 

    Toast.makeText(context, "this is not shown"  , Toast.LENGTH_LONG).show(); 



    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 

    Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show(); 


}