我正在尝试检测Android中的来电

时间:2017-01-26 15:09:41

标签: android broadcastreceiver

我只想在收到电话时显示祝酒词,但没有任何事情发生,在调试模式下它甚至不会停留在OnReceive。 我无法弄清楚为什么它没有停在onReceive上。就像它甚至没有收到意图一样。

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rajatgupta.broadcastrecieverexample">



<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="MyReciever">
        <intent-filter>
            <action android:name="rajat_action">
            </action>
        </intent-filter>
    </receiver>

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


</application>

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

IncomingCall.java

public class IncomingCall extends BroadcastReceiver {

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

      try {
   Log.d("Intent", "Intent Detected");
    Toast.makeText(context," Receiver start     ",Toast.LENGTH_SHORT).show();
}
catch (Exception e){
    e.printStackTrace();
}


}
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

仅仅将READ_PHONE_STATE权限放在Manifest中已经不够了,您现在必须在运行时请求权限。

许可检查: int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_PHONE_STATE);

请求: ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

https://developer.android.com/training/permissions/requesting.html