Search code examples
androidbroadcastreceiverhuawei-mobile-serviceshuawei-developers

BroadcastReceiver onReceive doesn't get called on Huawei Device


We have a requirement to show an acknoldgement screen after user share some texts on Messaging apps such as Whatsapp, Telegram, SMS etc..

We have been using the react-native-share library for this and it works fine on non Huawei devices. However, Huawei devices doesn't return success/failure when user share hence the app gives issues on Huawei devices.

Upon checking the react-native-share library internal codes, it seems the BroadcastReceiver onReceive method does not get triggered when user choose and app on HuaweiChooser.

I created the following sample code to simulate this and on Non-Huawei devices this code's onReceive method get triggered but not in Huawei devices.

Appreciate any help regarding this.

This is the Sample code

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button shareButton = findViewById(R.id.button);
        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent chooser;
                IntentSender intentSender = MyReceiver.getIntentSender(getApplicationContext());
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                sendIntent.setType("text/plain");
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                    chooser = Intent.createChooser(sendIntent,"Sharing Rocks",intentSender);
                    chooser.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                }else{
                    chooser = Intent.createChooser(sendIntent,"Sharing Rocks");
                }
                startActivityForResult(chooser, 1234);
            }
        });

    }
}

Receiver.Java

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    private static MyReceiver myReceiver;
    private static final String EXTRA_RECEIVER_TOKEN = "receiver_token";

    public static IntentSender getIntentSender(Context context){

        String intentAction = context.getPackageName()+"_ACTION";

        myReceiver = new MyReceiver();
        context.registerReceiver(myReceiver, new IntentFilter(intentAction));
        Intent intent = new Intent(intentAction);
        intent.setPackage(context.getPackageName());
        intent.putExtra(EXTRA_RECEIVER_TOKEN,myReceiver.hashCode());
        PendingIntent callback = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return callback.getIntentSender();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("//// Inside onReceive");
        ComponentName target = intent.getParcelableExtra(Intent.EXTRA_CHOSEN_COMPONENT);
        System.out.println("//// target : " + target);
    }
}

Manifest File

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

    <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/Theme.HuaweiShareTest">
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"></receiver>

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Solution

  • I change chooser title with "null" and my problem solved.

        val chooser = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            Intent.createChooser(sendIntent, null, pendingIntent.intentSender)
        } else {
            Intent.createChooser(sendIntent, null)
        }