BroadcastReceiver无法在发布模式下执行

时间:2018-08-20 09:15:39

标签: android android-broadcast android-broadcastreceiver android-sms

我尝试实现Request SMS Verification in an Android App,我的代码在调试中像魅力一样工作,但是当我生成sign apk应用并在发布时运行它时,我的BroadcastReceiver类似乎没有执行,我尝试了很多方法,例如将我的接收器定义为内部活动类别,但仍然是同样的问题!

应用程序标记中清单中的定义:

  <receiver android:name="com.hellow.CustomViews.BroadcastReceiverr" android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
            </intent-filter>
        </receiver>

这是我的接收者类:

public class BroadcastReceiverr extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {

            contextt = context;
            prefs = contextt.getSharedPreferences("MyPrefsFile", MODE_PRIVATE);
            editor = prefs.edit();



            Bundle extras = intent.getExtras();
            Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

            switch(status.getStatusCode()) {
                case CommonStatusCodes.SUCCESS:
                    // Get SMS message contents
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);


                        Toast.makeText(context, "done", Toast.LENGTH_SHORT).show();


                    Log.d("messageeeeee",message);
                    // Extract one-time code from the message and complete verification
                    // by sending the code back to your server.
                    break;
                case CommonStatusCodes.TIMEOUT:
                    // Waiting for SMS timed out (5 minutes)
                    // Handle the error ...
                    Log.d("Faileddd","dfg");
                    break;
            }
        }

这是SmsRetrieverClient的初始化:

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

        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d("onSuccess","onSuccessssssssssss");
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d("onFailure","onFailureeeeee");
            }
        });

但是我不知道接收器方法如何在调试中执行并吐司,但在发布时却没有!!

1 个答案:

答案 0 :(得分:0)

调试应用程序和发布应用程序的HashCode不同,请确保它们相同。

发布应用:

(在应用程序的根文件夹中)

  

keytool -exportcert -alias {keyAlias} -keystore {Keystore / jks文件} |   xxd -p | tr -d“ [:space:]” | echo -n {packageName} cat | shasum -a   256 | tr -d“ [:space:]-” | xxd -r -p | base64 |切-c1-11

调试应用:

(在〜/ .android中)

  

keytool -exportcert -alias androiddebugkey -keystore debug.keystore |   xxd -p | tr -d“ [:space:]” | echo -n {packageName} cat | shasum -a   256 | tr -d“ [:space:]-” | xxd -r -p | base64 |切-c1-11