我试图在接到电话后显示多士,我已经实现了注册广播接收器所需的所有必要操作,但它没有显示多士。我正在尝试在棉花糖设备上运行此程序

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

最佳答案

在棉花糖版本的情况下,我们有一个称为“运行时权限”的概念,该概念将在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;
        }
    }
}


这将提供一种使用棉花糖设备的方法

10-08 18:15