简体   繁体   English

Broadcastreceiver无法在android Nougat API 25级中工作吗?

[英]Broadcastreceiver not working in android Nougat api level 25?

I have got HTC-M8 and installed nougat on it api level 25. The app works fine on other devices like samsung grand prime and samsung galaxy s4 (api level 22,23) , but unfortunately not working on M8 why? 我已经安装了HTC-M8,并且在api级别25上安装了牛轧糖。该应用程序在其他设备(如三星大星际和samsung galaxy s4(api级别22,23))上都可以正常工作,但是不幸的是,为什么在M8上无法正常工作? Main Activity 主要活动

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

Manifest 表现

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

Starting with API 23 (Marshmallow) there are two changes which affect your code: 从API 23(棉花糖)开始,有两项更改会影响您的代码:

  1. In order to receive BluetoothDevice.ACTION_FOUND your app must have one of the location permissions (eg ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION ). 为了接收BluetoothDevice.ACTION_FOUND您的应用必须具有位置权限之一(例如ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION )。

  2. The permissions model changed in API 23, requiring runtime permission handling for dangerous level permissions, which both of the above location based permissions are. 在API 23中更改了权限模型,要求对dangerous级别权限进行运行时权限处理,以上两种基于位置的权限都是如此。

So you'll need to add one of the location permissions to your manifest and also have to add runtime permission handling. 因此,您需要向清单中添加位置权限之一,还必须添加运行时权限处理。 Or, if you are not ready to support API 23+, set your target API version to 22 or lower. 或者,如果您还不准备支持API 23+,请将目标API版本设置为22或更低。

Use these permissions in Android Manifest file. 在Android清单文件中使用这些权限。 Your issues will be solved. 您的问题将得到解决。

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android MediaPlayer 在 Nougat 中不起作用 - Android MediaPlayer not working in Nougat Intent.ACTION_CALL 不适用于 API 级别 25 及以下 - Intent.ACTION_CALL not working for API Level 25 and below Android:Java方法类型的API级别25,如果未调用会引起问题? - Android: java method type API level 25, cause issue if not called? 如何在Android API 25级及以下级别中使用Reflection API getParameters? - How do I Use Reflection API getParameters in Android API level 25 and Below? 如何修复 Android 中的“呼叫需要 API 级别 26(当前最小值为 25)”错误 - How to fix "Call requires API level 26 (current min is 25) " error in Android 在 Android &gt;= Api 26 中未调用 BroadcastReceiver - BroadcastReceiver is not called in Android >= Api 26 Android Nougat 7.1.1 broadcastReceiver 不接收动作 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION - Android Nougat 7.1.1 broadcastReceiver don't receive the action WifiManager.SCAN_RESULTS_AVAILABLE_ACTION ANDROID sendBroadcast到UNITY BroadcastReceiver无效 - ANDROID sendBroadcast to UNITY BroadcastReceiver not working Android 7.1.1上的Broadcastreceiver有时无法正常工作 - Broadcastreceiver on Android 7.1.1 sometimes not working Android BroadcastReceiver无法正常工作(包括错误) - Android BroadcastReceiver not working (error included)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM