标签: android-broadcastreceiver

使用BroadcastReceiver的Awareness API和Android O.

我有一个Android应用程序,它使用Awareness API在插入耳机时设置围栏.

我使用代码实现了AwarenessFence,就像以下示例中所示:https://developers.google.com/awareness/android-api/fence-register.

我有一个PendingIntent定义为:

PendingIntent.getBroadcast(context, 0, new Intent("my.application.packageFENCE_RECEIVER_ACTION"), 0)
Run Code Online (Sandbox Code Playgroud)

然后在我的AndroidManifest.xml文件中

<receiver android:name=".fence.FenceDetector$MyFenceReceiver">
<intent-filter>
    <action android:name="my.application.packageFENCE_RECEIVER_ACTION" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

这是在Manifest中声明的,因为即使我的应用程序在后台,我也希望接收广播.

这在Android 7.0及更低版本上运行良好,但是当我在Android 8.0上运行时,我收到错误:

BroadcastQueue: Background execution not allowed: receiving Intent { act=my.application.packageFENCE_RECEIVER_ACTION
Run Code Online (Sandbox Code Playgroud)

我认为这是由于Android O上后台执行的新限制.

任何人都可以告诉我如何注册一个广播接收器,它可以在运行API 26的Android设备上在后台监听感知围栏触发器.

让我知道如果有些事情不清楚或者我需要详细说明.

提前致谢

android broadcastreceiver android-broadcastreceiver google-awareness android-8.0-oreo

16
推荐指数
2
解决办法
3816
查看次数

在Play商店中正确跟踪安装推介

我有一个简单的任务:我想跟踪应用安装的引用ID并将其传递给后端.

我做了什么:我创建了一个带有额外参数的链接,referrer并将其附加到邀请链接.当它打开时,javascript检测浏览器是否是Android移动浏览器,然后准备intent并发出重定向到该意图.在准备意图时,referrer字段从网址中提取并附加到intent以下内容中:

intent://scan/#Intent;scheme=com.example.android;package=com.example.android&referrer=4;end
Run Code Online (Sandbox Code Playgroud)

这是我的代码BroadCastReceiver:

public class InstallReferrerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        TinyDB tinyDB = new TinyDB(context);
        String referrer = intent.getStringExtra("referrer");
        tinyDB.putString(AppConstants.REFERRAL_ID, referrer);
        tinyDB.putBoolean(AppConstants.REFERRAL_SENT, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我期望在这里得到的价值referrer4基于上述intent.但我得到的价值就是这个字符串utm_source=google-play&utm_medium=organic

我做错了什么,如何修复它以获得正确的referrer字段值?

编辑

referrer安装应用程序后,我在创建URL或从字段中提取值时没有任何问题.

通过点击任意按钮或直接在移动浏览器中打开邀请链接后,我使用上述内容"如果已经安装了应用程序,请打开应用程序,或者在Play商店应用程序上打开应用程序页面以供用户安装".

问题是,我应该如何通过上述意图将referrer字段的值从邀请链接传递到Play商店应用,以便Play商店收到此值并在安装时将其传递给应用.

android android-intent install-referrer android-broadcastreceiver

15
推荐指数
2
解决办法
1万
查看次数

动态注册广播接收器不起作用--BluetoothDevice.ACTION_FOUND

Logclass跟踪运行时显示onReceive()方法没有调用,为什么?

动态注册广播接收器

 private void discoverDevices () {
    Log.e("MOHAB","BEFORE ON RECEIVE");

     mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            Log.e("MOHAB","ON RECEIVE");
            String action = intent.getAction();
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                Bluetooth b = new Bluetooth(device.getName(),device.getAddress());
                list.add(b);
            }
        }
    };
    Log.e("MOHAB","create intentFilter");
    // …
Run Code Online (Sandbox Code Playgroud)

android android-broadcastreceiver

13
推荐指数
4
解决办法
1万
查看次数

自我更新时崩溃APK(组件类不存在)

我正在研究一个系统应用程序,它通过下载apk并使用PackageManager的installPackage()方法安装它来更新它.

我收到以下异常:

Fatal Exception: java.lang.IllegalArgumentException: Component class com.myapp.package.receivers.SomeOldReceiver does not exist in com.myapp.package
           at android.os.Parcel.readException(Parcel.java:1544)
           at android.os.Parcel.readException(Parcel.java:1493)
           at android.content.pm.IPackageManager$Stub$Proxy.setComponentEnabledSetting(IPackageManager.java:3420)
           at android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:1492)
           at com.myapp.package.utils.AndroidUtils.enableDisableComponent(SourceFile:113)
           at ...
Run Code Online (Sandbox Code Playgroud)

更新的apk中不存在"SomeOldReceiver"组件.

似乎现有APK的"旧"代码被执行(其应用程序的onCreate()被调用)试图访问更新的APK中存在的"新"清单,并且找不到"旧"接收器(这是甚至可能?).

我的应用程序还会收听PACAKGE_ADDED和PACKAGE_REMOVED意图.

此崩溃发生在相对较大比例的用户身上.

关于为什么会发生这种错误以及如何修复它的任何建议或线索将不胜感激.

android android-manifest android-install-apk android-broadcastreceiver

13
推荐指数
1
解决办法
763
查看次数

在Android 6.0上,Alarm Manager无法在后台运行

这是我的Activity代码,

Long time = new GregorianCalendar().getTimeInMillis()+20000;//Setting alarm after 20 sec
Intent intentAlarm = new Intent("alarm");
intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentAlarm.putExtra("req_code",10);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,10, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

这些是我在我的应用中拥有的所有权限,

  <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="com.myapp.pack.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)

这是我的BroadcastReceiver代码,

@Override
public void onReceive(Context context, Intent intent) {
        SharedPreferences sharedPreferences =
        context.getSharedPreferences( "mydata", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("elligible",true);
        editor.apply();

    }
Run Code Online (Sandbox Code Playgroud)

BroadcastReceiver在清单上注册了我的,

 <receiver …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-alarms android-broadcast android-broadcastreceiver

12
推荐指数
2
解决办法
1万
查看次数

通过蓝牙耳机接听电话

我正在开发VoIP-Android-App.我想通过活动中的连接蓝牙耳机接受和拒绝来电.

到目前为止我尝试了什么:

  • 使用媒体会话接收媒体按钮点击.

    问题:如果我们启动BluetoothSCO,我们不会收到任何Media Button点击.如果我们不启动BluetoothSCO,我们会收到Media Button点击,但我们无法区分长按和短按,因为停机时间始终为0,键码始终为KEYCODE_MEDIA_PLAY,ACTION_DOWN后面紧跟ACTION_UP.只有通过蓝牙连接时才会出现这些问题.如果我们通过有线耳机连接,我们会获得相应的密钥代码(KEYCODE_HEADSETHOOK),停机时间不为0.

  • 使用BroadcastReceiver监听蓝牙SCO连接的变化.

    private val scoReceiver = object : BroadcastReceiver() {
        fun onReceive(context: Context, intent: Intent) {
            val state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1)
            val previousState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, -1)
            if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED && previousState == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
                Log.e(TAG, "SCO Disconnected")
                hangupCall()
            }
        }
    }
    
    protected fun onStart() {
        super.onStart()
        val intentFilter = IntentFilter()
        intentFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)
        registerReceiver(scoReceiver, intentFilter)
    }
    
    Run Code Online (Sandbox Code Playgroud)

    通过这种方法,我可以检测用户何时挂断电话,例如长按蓝牙耳机,因为这会触发SCO断开连接.

    问题:我们无法检测用户是否想接听来电.

  • 使用dispatchKeyEvent,onKeyDownonKeyUp.

    问题:他们从来没有被召唤过.

有没有人有任何建议或最佳实践如何正确处理蓝牙耳机?非常感谢任何帮助.提前致谢!

android headset android-bluetooth android-broadcastreceiver voip-android

12
推荐指数
1
解决办法
1030
查看次数

在Android中叠加窗口服务

我必须像True Caller App一样实现Overlay Window.But我得到的问题是,在任何Incoming或Outgoing呼叫期间,我的服务会自动关闭或销毁.

服务类

public class OverlayService extends Service implements View.OnClickListener,NotifyHardwareChanges,UpdateSoundDB{

private WindowManager windowManager;
WindowManager.LayoutParams params;
View view;
Button btnEndCall;
public static TextView textView;
public static Context cntxt;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    return START_NOT_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    this.cntxt = getApplicationContext();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    params= new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE, …
Run Code Online (Sandbox Code Playgroud)

android popup android-service android-service-binding android-broadcastreceiver

11
推荐指数
1
解决办法
2029
查看次数

无法从小米设备的后台唤醒应用程序,直到手动启用"自动启动"

在小米的MI设备上,有一个功能是在他们的安全应用程序中关闭/打开"自动启动".(在安全性应用程序 - >权限 - >自动启动)

在此输入图像描述

这意味着当应用程序未运行时,任何广播接收器都不会收到任何内容.所以BOOT_COMPLETED,USER_PRESENT,CONNECTIVITY_CHANGE等......都行不通.(他们在应用程序处于前台后工作了一段时间,但很快就停止了).用户从小米的"最近的应用程序"版本中刷出应用程序后,它们也停止工作

即使GCM也无法将其唤醒

对于消息传递应用程序,这是一个杀手.

默认情况下,Whatsapp,Messenger,Flipkart等应用程序默认启用(即使这些应用程序未预先安装).

大多数其他应用程序默认禁用此功能.例如.默认情况下禁用Slack.

有没有办法默认进入这个白名单?

android android-service android-broadcast android-broadcastreceiver

10
推荐指数
1
解决办法
692
查看次数

Boot BroadcastReceiver无法在小米设备上运行

我有一个跟随BroadcastReceiver,应该在启动完成后运行.我已经在我的小米设备(Redmi 1s)上进行了测试,它没有运行,而在像三星这样的其他设备上它按预期运行.

public class DeviceBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Toast.makeText(context, "I am Running", Toast.LENGTH_SHORT).show();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已在Manifest中设置了权限.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Run Code Online (Sandbox Code Playgroud)

以下是我的广播接收器:

<receiver android:name=".receiver.DeviceBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

android broadcastreceiver android-permissions android-reboot android-broadcastreceiver

10
推荐指数
1
解决办法
3181
查看次数

为什么SMS Retriever API在发布模式下不起作用?

我已经SMS Retriever API在谷歌教程和我的调试Build Variant工作中实现了类似的工作.我可以读取短信并获取用户可以登录的代码.

我的问题是,当我在发布版本Variant中运行应用程序sms它不起作用.我收到了短信,但我无法读取代码进行登录.

我在发布模式下更改了AppSignatureHelper生成的哈希,该模式与调试模式不同.在调试工作和发布没有.

一些帮助将是欣赏

代码:

表现:

   <receiver android:name=".app.receivers.SmsReceiver">
        <intent-filter>
            <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

在我的类中:(在发布和调试模式下,代码将抛出onSucess方法)此方法在onCreate中调用.

private void startSMSListening(){
    SmsRetrieverClient client = SmsRetriever.getClient(this);
    Task<Void> task = client.startSmsRetriever();

    task.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            // Successfully started retriever, expect broadcast intent
            Log.e("startSMSListening", "listening sms");
            sendCode();
            showHideLoadingView(false);
        }
    });

    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            // Failed to start retriever, inspect Exception for more details
            Log.e("startSMSListening", "failure listening …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-broadcast android-broadcastreceiver android-sms

10
推荐指数
3
解决办法
3864
查看次数