gpt4 book ai didi

java - Android 调用广播接收器无法正常工作? (棉花糖)

转载 作者:行者123 更新时间:2023-12-01 07:20:39 25 4
gpt4 key购买 nike

我试图在接到电话后显示 toast,我已经实现了注册广播接收器所需的所有必要内容,但它没有显示 toast。我正在尝试在 Marshmallow 设备上运行此程序

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 -

<?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

最佳答案

对于 Marshmallow 版本,我们有一个称为运行时权限的概念,它是在 Activity 内部创建的,以便使用该权限。运行时权限提供了一种在用户第一次运行 Activity 时在运行时请求特定权限的方法。

您必须指定两件事:

//指定任意常量的权限

  public final static int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 11;

//在 OnCreate 方法中指定以下代码

 @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);
}
}
}

//指定该方法,运行时会弹出窗口询问用户权限

@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 设备的方式

关于java - Android 调用广播接收器无法正常工作? (棉花糖),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com