应用程序在 USB 调试模式下工作正常,但是当我生成签名 APK 并安装它时,它不起作用 [英] App is working fine in usb debug mode, but when i generate Signed APK and install it, its not working

查看:44
本文介绍了应用程序在 USB 调试模式下工作正常,但是当我生成签名 APK 并安装它时,它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了不同的解决方案,例如在清单文件中提供 INTERNET PERMISION &&通过 useProguard false 关闭 PROGUARD

I have tried different solutions like giving INTERNET PERMISION in manifest file && Turning off PROGUARD by useProguard false

我的应用名称:SMS_VOICE_NOTIFICATION

My app name: SMS_VOICE_NOTIFICATION

我的应用程序描述:因此,每当我的手机收到短信时,它都会使用文本转语音读出 smsbody,为此我使用前台服务,因为如果我使用后台服务,我的系统会在一段时间后将其杀死.

My app description: so whenever my phone receive an SMS it reads out the smsbody using Text-to-Speech, For this im using foreground service because if i use background service my system kills it after some Time.

在调试模式下一切正常,但是当我开始生成签名的 apk 时,广播接收器不工作,只是 UI 部分在工作

In debug mode everything is working great, but when I start generating signed apk the broadcast receiver is not working just the UI part is working

我已经像这样注册了广播接收器.

I have registered the Broadcast Receiver like this.

public class MainActivity extends AppCompatActivity {
private MyReceiver receiver;

@Override
protected void onStart() {

    IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    receiver = new MyReceiver();
    registerReceiver(receiver,intentFilter);
    super.onStart();
} 

build.gradle(app)

build.gradle(app)

defaultConfig {
    applicationId "com.example.smsassistsigiri"
    minSdkVersion 23
    targetSdkVersion 30
    multiDexEnabled true
    versionCode 1
    versionName "1.0.1"
    resConfigs "en"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        useProguard false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

我的 build.gradle 文件

My build.gradle file

defaultConfig {
    applicationId "com.example.smsassistsigiri"
    minSdkVersion 23
    targetSdkVersion 30
    multiDexEnabled true
    versionCode 1
    versionName "1.0.1"
    resConfigs "en"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        useProguard false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

我的广播接收器类

public class MyReceiver extends BroadcastReceiver {

private static  final String SMS_RECEIVED =  "android.provider.Telephony.SMS_RECEIVED";
//TODO:Implement whats app text to speech
public static final String WHATS_APP_RECEIVED = "";
private static final String TAG = "SmsBroadcastReceiver";
String msg,PhoneNo = "";


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

    Log.i(TAG,"INTENT RECEIVED:"+intent.getAction());
    //if(intent.getAction()==SMS_RECEIVED){
      if(intent.getAction().equals(SMS_RECEIVED)){
        //Retrieve  a map of extended data from the intent
        Bundle dataBundle = intent.getExtras();
        if(dataBundle!=null){
            //creating a PDU (PROTOCOL DATA UNIT)
            Object[]  mypdu = (Object[]) dataBundle.get("pdus");
            final SmsMessage[] message = new SmsMessage[mypdu.length];
            for(int i=0;i<mypdu.length;i++){
                if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                    //API leve  > than 23
                    String format = dataBundle.getString("format");
                    message[i] = SmsMessage.createFromPdu((byte[])mypdu[i],format);
                }
                else{
                    message[i] = SmsMessage.createFromPdu((byte[])mypdu[i]);
                }
                msg = message[i].getMessageBody();
                PhoneNo = message[i].getOriginatingAddress();
            }

            SmsService instance = SmsService.getInstance();
            //if startservice button is not pressed then if we get a message then instance will be null so that's why we need to check for null
            if(instance!=null){
                instance.speak(msg);
            }
            Toast.makeText(context, "Message: "+msg+" \nNumber: "+PhoneNo, Toast.LENGTH_SHORT).show();


        }
        else{
            Toast.makeText(context, "Data bundle is null", Toast.LENGTH_SHORT).show();
        }
    }
      else {
          Log.e("SMSNOTRECEIVED","SMSNOTRECEIVED");
      }
}

}

主要文件

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<application
    android:name=".App"
    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.SMSAssist">
    <receiver android:name=".MyReceiver" android:enabled="true" android:exported="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" ></action>
    </intent-filter>
    </receiver>


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

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

推荐答案

您的应用是否已注册为设备的默认短信或助理处理程序?除非您的应用是默认处理程序,否则您将无法再使用 READ_SMS 等权限:

Is your app registered as the device's default SMS or assistant handler? You're no longer able to use permissions like READ_SMS unless your app is the default handler:

https://support.google.com/googleplay/android-developer/answer/10208820?visit_id=637627208051926352-3034324628&rd=1#zippy=%2Cpermitted-uses-of-the-sms-and-call-log-permissions

这篇关于应用程序在 USB 调试模式下工作正常,但是当我生成签名 APK 并安装它时,它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆