[英]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(棉花糖)开始,有两项更改会影响您的代码:
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_LOCATION
或ACCESS_FINE_LOCATION
)。
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.