[英]App is working fine in usb debug mode, but when i generate Signed APK and install it, its not working
我尝试了不同的解决方案,例如在清单文件中提供 INTERNET PERMISION && Turning off PROGUARD by useProguard false
我的应用名称:SMS_VOICE_NOTIFICATION
我的应用程序描述:因此,每当我的手机收到 SMS 时,它都会使用文本转语音读出 smsbody,为此我使用前台服务,因为如果我使用后台服务,我的系统会在一段时间后将其杀死。
在调试模式下一切正常,但是当我开始生成签名的 apk 时,广播接收器不工作,只是 UI 部分在工作
我已经像这样注册了广播接收器。
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(应用程序)
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 文件
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>
在应用已签名的 android 构建混淆时,有时会删除应用程序代码的必要部分。 因此,如果您不使用模拟器,请连接测试设备并仔细观察 logcat,您可以在其中看到应用程序代码中断的问题。 现在,您需要添加不想混淆代码的例外情况。 为了这 :
打开 proguard-rules.pro 文件,该文件位于 build.gradle 文件下方的应用程序目录下。
现在通过添加导致崩溃的类来添加例外情况,如下所示:
-keep public class com.app.YourActivtyOrFragment { public <methods>; public <fields>; }
您的应用是否已注册为设备的默认短信或助理处理程序? 除非您的应用是默认处理程序,否则您将无法再使用 READ_SMS 等权限:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.