Broadcastreciver无法在Android Nougat api 25级工作?

时间:2017-06-28 19:39:50

标签: java android android-7.0-nougat android-api-levels

我有HTC-M8并在它的api 25级安装了牛轧糖。 该应用程序在三星Grand prime和三星glaxy s4(api level 22,23)等其他设备上运行良好,但遗憾的是没有在M8上工作为什么? 主要活动

package com.example.idani.phoni;

import android.Manifest;
import android.app.ActionBar;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class Home extends AppCompatActivity {
    BluetoothAdapter bluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


        IntentFilter inFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        BlueReciver bReciver = new BlueReciver();

        registerReceiver(bReciver,inFilter);

        bluetoothAdapter.startDiscovery();






    }
}

BlueReciver.java

package com.example.idani.phoni;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by iDani on 6/28/2017.
 */

public class BlueReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        BluetoothDevice dv = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Toast.makeText(context,"HI FOUND SOMETHING"+dv.getName(),Toast.LENGTH_LONG).show();
    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.idani.phoni">
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"

        >



        <activity android:name=".Home"

            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

从API 23(Marshmallow)开始,有两个更改会影响您的代码:

  1. 要接收BluetoothDevice.ACTION_FOUND,您的应用必须拥有其中一个位置权限(例如ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION)。

  2. API 23中的权限模型已更改,需要对dangerous级权限进行运行时权限处理,而上述两种权限都属于此权限。

  3. 因此,您需要向清单添加一个位置权限,还必须添加运行时权限处理。或者,如果您尚未准备好支持API 23+,请将目标API版本设置为22或更低。

答案 1 :(得分:0)

在Android Manifest文件中使用这些权限。你的问题将得到解决。

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />