2016-08-10 20 views
0

受信した着信のトーストを表示しようとしています。しかし、私は表示を取得していません。マニフェストファイル内の受信者には、電話に必要な許可も含まれています。 以下は私が使用したコードです。着信のための放送受信機が機能していませんか?

// inside IncomingCall broadcastreceiver 
package com.example.shailesh.callbroadcastreceiver; 

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

public class IncomingCall extends BroadcastReceiver { 

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

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
     // This code will execute when the phone has an incoming call 

     // get the phone number 
     String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); 
     Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show(); 

    } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_IDLE) 
      || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
      TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
     // This code will execute when the call is disconnected 
     Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show(); 

    } 

    } 
} 

、次のように私はmenifestファイルに指定しています

<?xml version="1.0" encoding="utf-8"?> 

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


<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" /> 
     </intent-filter> 
    </activity> 

    <receiver android:name=".IncomingCall" android:enabled="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
    </receiver> 
</application> 

私は、着信コールのトースト表示を得るために含める必要がありますほかに何か受け取った。私はまた私のMainActivity

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent i = new Intent(getApplicationContext(),IncomingCall.class); 
    startService(i); 
} 
} 
+0

possibel duplicate of http://stackoverflow.com/a/27107056/5515371 –

+0

受信者にパッケージ名を追加する

+0

まだ動作していません –

答えて

2

に次のコードが含まれている はIncomingCallサービスクラスですか?そうでなければ、なぜあなたはサービスを開始する意図を発していますか?

+0

私はこれらの行を削除して、プロジェクトを再実行しても、電話を受けると出力が出ません。 –

関連する問題