用于通话的 Android 广播接收器不起作用?(棉花糖) [英] Android Broadcast receiver for call not working? (Marshmallow)
问题描述
我正在尝试在接到电话后显示 toast,我已经实现了注册广播接收器所需的所有必要内容,但它没有显示 toast.我正在尝试在 Marshmallow 设备上运行此程序
I am trying to display toast after receiving an call, I have implemented all necessary things needed to register broadcast receiver but it is not displaying toast. I am trying to run this program on Marshmallow device
MyCallReceiver.java -
MyCallReceiver.java -
package com.suhas.callreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyCallReceiver 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();
Log.d("MyTrack call", "call receive");
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
}
}
}
AndroidManifest.xml -
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suhas.msgmanager">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/msgis"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.suhas.msgmanager.MyDialog" android:launchMode="singleTask"
android:theme="@android:style/Theme.Translucent" />
<service android:name="com.example.suhas.msgmanager.ChatHeadService"></service>
<receiver android:name=".MyCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity android:name=".AddMessageActivity">
</activity>
</application>
</manifest>
我有一个 MainActivity
带有一个默认标签,上面写着 Hello World
.
I have one MainActivity
with one default label saying Hello World
.
推荐答案
在 Marshmallow 版本的情况下,我们有一个叫做 Runtime 权限的概念,它是在 Activity 内部创建的,以便使用该权限.运行时权限提供了一种在用户第一次运行活动时在运行时询问特定权限的方法.
In Case of Marshmallow Version, We have a concept called Runtime permission which is to be made inside Activity in order to work with the permission. Runtime permission provides a way to ask user for particular permission at runtime while he runs activity for first time.
这是您必须指定的两件事:
This are two things you have to specify :
//为权限指定任意常数
//specify any constant number for permission
public final static int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 11;
//在 OnCreate 方法中指定以下代码
// Specify following bit of code in OnCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CONTACTS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
}
}
//指定这个方法会在运行时弹出窗口询问用户权限
//specify this method which will popup window asking user for permission at runtime
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
}
return;
}
}
}
这将提供一种使用 Marshmallow 设备的方法
this will provide a way to work with Marshmallow devices
这篇关于用于通话的 Android 广播接收器不起作用?(棉花糖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!