如何在android 6中使用广播接收器启动应用程序,该应用程序不';我没有许可

如何在android 6中使用广播接收器启动应用程序,该应用程序不';我没有许可,android,broadcastreceiver,android-6.0-marshmallow,android-permissions,Android,Broadcastreceiver,Android 6.0 Marshmallow,Android Permissions,如何在我的手机接到呼叫时启动我的应用程序。广播接收器无法工作,因为我的应用程序之前没有运行,而且我没有权限检查android 6.0(棉花糖)中的android.permission.READ_PHONE_状态 我没有任何活动课。我只是想在接到电话时创建一个txt文件 我只是想知道,当应用程序尚未启动时,我们如何才能获得广播接收器的许可。当呼叫到来时,广播将不起作用,因为我们没有权限 这是我的密码: Android清单 <?xml version="1.0" encoding="utf-8

如何在我的手机接到呼叫时启动我的应用程序。广播接收器无法工作,因为我的应用程序之前没有运行,而且我没有权限检查android 6.0(棉花糖)中的android.permission.READ_PHONE_状态

我没有任何活动课。我只是想在接到电话时创建一个txt文件

我只是想知道,当应用程序尚未启动时,我们如何才能获得广播接收器的许可。当呼叫到来时,广播将不起作用,因为我们没有权限

这是我的密码:

Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ranveer.teaser">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".IncommingCallReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

除非你的应用程序是系统应用程序,否则你需要至少有一个
活动
,用户需要明确(手动)启动你的应用程序至少一次。否则,您的应用程序将保持“停止状态”,并且您的
BroadcastReceiver
将不会收到对
onReceive()
的任何调用。自Android 3.0以来,情况一直如此。

您需要一个
活动
。事情就是这样。
 package com.example.ranveer.teaser;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.TelephonyManager;
    import java.io.*;
    import android.widget.Toast;
    public class IncommingCallReceiver extends BroadcastReceiver 
    {
        public IncommingCallReceiver() 
        {
        }
        Context mContext;

        @Override
        public void onReceive(Context mContext, Intent intent)
        {
            try{
             File file = new File("Hello1.txt");
             file.createNewFile();
              FileWriter writer = new FileWriter(file);

        // Writes the content to the file
        writer.write("This\n is\n an\n example\n");
        writer.flush();
        writer.close();
            Toast.makeText(mContext,"Done writing in file",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(mContext, e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }

            try
            {

                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);



                if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
                {
                    Toast.makeText(mContext, "Phone Is Ringing", Toast.LENGTH_LONG).show();
                    // Your Code
                }

                if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
                {
                    Toast.makeText(mContext, "Call Recieved", Toast.LENGTH_LONG).show();
                    // Your Code
                }

                if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
                {

                    Toast.makeText(mContext, "Phone Is Idle", Toast.LENGTH_LONG).show();
                    // Your Code

                }
            }
            catch(Exception e)
            {
                //your custom message
            }

        }
    }