修复:无标题通知导致空指针FC

整理:代码梳理,删除无用图标
pull/86/head
pppscn 3 years ago
parent 1882443280
commit e0053c4ec8

@ -31,6 +31,7 @@ import com.idormy.sms.forwarder.utils.PhoneUtils;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsUtil;
import com.idormy.sms.forwarder.utils.TimeUtil;
import com.umeng.analytics.MobclickAgent;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -67,9 +68,13 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
NetUtil.init(this);
//前台服务
serviceIntent = new Intent(MainActivity.this, FrontService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(serviceIntent);
try {
serviceIntent = new Intent(MainActivity.this, FrontService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(serviceIntent);
} catch (Exception e) {
Log.e(TAG, "onCreate:", e);
}
}
@Override
@ -147,6 +152,8 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
@Override
protected void onResume() {
super.onResume();
MobclickAgent.onResume(this);
//第一次打开未授权无法获取SIM信息尝试在此重新获取
if (MyApplication.SimInfoList.isEmpty()) {
MyApplication.SimInfoList = PhoneUtils.getSimMultiInfo();
@ -156,7 +163,7 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
//省电优化设置为无限制
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!KeepAliveUtils.isIgnoreBatteryOptimization(this)) {
Toast.makeText(this, "请将省电优化设置为无限制(不优化),有利于《短信转发器》保活!", Toast.LENGTH_LONG).show();
Toast.makeText(this, R.string.tips_battery_optimization, Toast.LENGTH_LONG).show();
}
}
@ -164,22 +171,36 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
if (SettingUtil.getSwitchEnableAppNotify() && !CommonUtil.isNotificationListenerServiceEnabled(this)) {
CommonUtil.toggleNotificationListenerService(this);
SettingUtil.switchEnableAppNotify(false);
Toast.makeText(this, "请先授予《短信转发器》通知使用权否则无法转发APP通知已经自动关闭转发!", Toast.LENGTH_LONG).show();
Toast.makeText(this, R.string.tips_notification_listener, Toast.LENGTH_LONG).show();
return;
}
startService(serviceIntent);
try {
if (serviceIntent != null) startService(serviceIntent);
} catch (Exception e) {
Log.e(TAG, "onResume:", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
startService(serviceIntent);
try {
if (serviceIntent != null) startService(serviceIntent);
} catch (Exception e) {
Log.e(TAG, "onDestroy:", e);
}
}
@Override
protected void onPause() {
super.onPause();
startService(serviceIntent);
MobclickAgent.onPause(this);
try {
if (serviceIntent != null) startService(serviceIntent);
} catch (Exception e) {
Log.e(TAG, "onPause:", e);
}
}
@Override
@ -187,10 +208,10 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CommonUtil.NOTIFICATION_REQUEST_CODE) {
if (CommonUtil.isNotificationListenerServiceEnabled(this)) {
Toast.makeText(this, "通知服务已开启", Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.notification_listener_service_enabled, Toast.LENGTH_SHORT).show();
CommonUtil.toggleNotificationListenerService(this);
} else {
Toast.makeText(this, "通知服务未开启", Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.notification_listener_service_disabled, Toast.LENGTH_SHORT).show();
}
}
}

@ -75,27 +75,31 @@ public class MyApplication extends Application {
public void onCreate() {
Log.d(TAG, "onCreate");
super.onCreate();
//初始化组件化基础库, 所有友盟业务SDK都必须调用此初始化接口。
//建议在宿主App的Application.onCreate函数中调用基础组件库初始化函数。
UMConfigure.init(this, "60254fc7425ec25f10f4293e", getChannelName(this), UMConfigure.DEVICE_TYPE_PHONE, "");
// 选用LEGACY_AUTO页面采集模式
MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_MANUAL);
//pro close log
UMConfigure.setLogEnabled(true);
Intent intent = new Intent(this, FrontService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
SendHistory.init(this);
SettingUtil.init(this);
try {
//初始化组件化基础库, 所有友盟业务SDK都必须调用此初始化接口。
//建议在宿主App的Application.onCreate函数中调用基础组件库初始化函数。
UMConfigure.init(this, "60254fc7425ec25f10f4293e", getChannelName(this), UMConfigure.DEVICE_TYPE_PHONE, "");
// 选用LEGACY_AUTO页面采集模式
MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_MANUAL);
//pro close log
UMConfigure.setLogEnabled(true);
EmailKit.initialize(this);
Intent intent = new Intent(this, FrontService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
SendHistory.init(this);
SettingUtil.init(this);
SharedPreferences sp = MyApplication.this.getSharedPreferences(Define.SP_CONFIG, Context.MODE_PRIVATE);
showHelpTip = sp.getBoolean(Define.SP_CONFIG_SWITCH_HELP_TIP, true);
EmailKit.initialize(this);
SharedPreferences sp = MyApplication.this.getSharedPreferences(Define.SP_CONFIG, Context.MODE_PRIVATE);
showHelpTip = sp.getBoolean(Define.SP_CONFIG_SWITCH_HELP_TIP, true);
} catch (Exception e) {
Log.e(TAG, "onCreate:", e);
}
}
}

@ -361,7 +361,7 @@ public class SettingActivity extends AppCompatActivity {
if (!CommonUtil.isNotificationListenerServiceEnabled(this)) {
CommonUtil.openNotificationAccess(this);
} else {
Toast.makeText(this, "通知服务已开启", Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.notification_listener_service_enabled, Toast.LENGTH_SHORT).show();
CommonUtil.toggleNotificationListenerService(this);
}
}
@ -371,11 +371,16 @@ public class SettingActivity extends AppCompatActivity {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CommonUtil.NOTIFICATION_REQUEST_CODE) {
if (CommonUtil.isNotificationListenerServiceEnabled(this)) {
Toast.makeText(this, "通知服务已开启", Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.notification_listener_service_enabled, Toast.LENGTH_SHORT).show();
CommonUtil.toggleNotificationListenerService(this);
SettingUtil.switchEnableAppNotify(true);
} else {
Toast.makeText(this, "通知服务未开启", Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.notification_listener_service_disabled, Toast.LENGTH_SHORT).show();
SettingUtil.switchEnableAppNotify(false);
}
@SuppressLint("UseSwitchCompatOrMaterialCode") Switch switch_enable_app_notify = findViewById(R.id.switch_enable_app_notify);
switch_enable_app_notify.setChecked(SettingUtil.getSwitchEnableAppNotify());
}
}

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
@ -40,6 +41,7 @@ public class AppAdapter extends ArrayAdapter<AppInfo> {
return 0;
}
@SuppressLint("SetTextI18n")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppInfo appInfo = getItem(position); //获取当前项的TLog实例

@ -3,6 +3,8 @@ package com.idormy.sms.forwarder.model;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import lombok.Data;
@Data
@ -34,6 +36,7 @@ public class AppInfo {
this.verCode = verCode;
}
@NonNull
@Override
public String toString() {
return "AppInfo{" +

@ -41,38 +41,47 @@ public class NotifyService extends NotificationListenerService {
//自身通知跳过
if ("com.idormy.sms.forwarder".equals(packageName)) return;
//通知标题
String title = sbn.getNotification().extras.get("android.title").toString();
//通知内容
String text = sbn.getNotification().extras.get("android.text").toString();
if (text.isEmpty() && sbn.getNotification().tickerText != null) {
text = sbn.getNotification().tickerText.toString();
try {
//通知标题
String title = "";
if (sbn.getNotification().extras.get("android.title") != null) {
title = sbn.getNotification().extras.get("android.title").toString();
}
//通知内容
String text = "";
if (sbn.getNotification().extras.get("android.text") != null) {
text = sbn.getNotification().extras.get("android.text").toString();
}
if (text.isEmpty() && sbn.getNotification().tickerText != null) {
text = sbn.getNotification().tickerText.toString();
}
//不处理空消息(标题跟内容都为空)
if (title.isEmpty() && text.isEmpty()) return;
//通知时间
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).format(new Date(sbn.getPostTime()));
Log.d(TAG, String.format(
Locale.getDefault(),
"onNotificationPosted\n应用包名%s\n消息标题%s\n消息内容%s\n消息时间%s\n",
packageName, title, text, time)
);
//重复通知不再处理
String prevHash = SettingUtil.getPrevNoticeHash(packageName);
String currHash = CommonUtil.MD5(packageName + title + text + time);
Log.d(TAG, "prevHash=" + prevHash + " currHash=" + currHash);
if (prevHash != null && prevHash.equals(currHash)) {
Log.w(TAG, "重复通知不再处理");
return;
}
SettingUtil.setPrevNoticeHash(packageName, currHash);
SmsVo smsVo = new SmsVo(packageName, text, new Date(), title);
Log.d(TAG, "send_msg" + smsVo.toString());
SendUtil.send_msg(this, smsVo, 1, "app");
} catch (Exception e) {
Log.e(TAG, "onNotificationPosted:", e);
}
//不处理空消息(标题跟内容都为空)
if (title.isEmpty() && text.isEmpty()) return;
//通知时间
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE).format(new Date(sbn.getPostTime()));
Log.d(TAG, String.format(
Locale.getDefault(),
"onNotificationPosted\n应用包名%s\n消息标题%s\n消息内容%s\n消息时间%s\n",
packageName, title, text, time)
);
//重复通知不再处理
String prevHash = SettingUtil.getPrevNoticeHash(packageName);
String currHash = CommonUtil.MD5(packageName + title + text + time);
Log.d(TAG, "prevHash=" + prevHash + " currHash=" + currHash);
if (prevHash != null && prevHash.equals(currHash)) {
Log.w(TAG, "重复通知不再处理");
return;
}
SettingUtil.setPrevNoticeHash(packageName, currHash);
SmsVo smsVo = new SmsVo(packageName, text, new Date(), title);
Log.d(TAG, "send_msg" + smsVo.toString());
SendUtil.send_msg(this, smsVo, 1, "app");
//NotifyHelper.getInstance().onReceive(sbn);
}

@ -74,7 +74,7 @@
android:background="@color/colorPrimary"
android:onClick="cleanLog"
android:text="@string/bt_refresh_log"
tools:ignore="ButtonStyle,NestedWeights" />
tools:ignore="ButtonStyle,NestedWeights,UsingOnClickInXml" />
<Button
android:layout_width="match_parent"
@ -86,7 +86,7 @@
android:background="@color/colorPrimary"
android:onClick="toRuleSetting"
android:text="@string/rule_setting"
tools:ignore="ButtonStyle,NestedWeights" />
tools:ignore="ButtonStyle,NestedWeights,UsingOnClickInXml" />
<Button
android:layout_width="match_parent"
@ -99,7 +99,7 @@
android:background="@color/colorPrimary"
android:onClick="toSendSetting"
android:text="@string/sender_setting"
tools:ignore="ButtonStyle" />
tools:ignore="ButtonStyle,UsingOnClickInXml" />
</LinearLayout>
</LinearLayout>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 321 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 535 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 253 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 385 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 466 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 807 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 603 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 837 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

@ -217,4 +217,8 @@
<string name="appicon">App Icon</string>
<string name="user_app">User App</string>
<string name="system_app">System App</string>
<string name="tips_battery_optimization">请将省电优化设置为无限制(不优化),有利于《短信转发器》保活!</string>
<string name="tips_notification_listener">请先授予《短信转发器》通知使用权否则无法转发APP通知已经自动关闭转发!</string>
<string name="notification_listener_service_enabled">通知服务已开启</string>
<string name="notification_listener_service_disabled">通知服务未开启</string>
</resources>

@ -216,4 +216,8 @@
<string name="appicon">应用图标</string>
<string name="user_app">用户应用</string>
<string name="system_app">系统应用</string>
<string name="tips_battery_optimization">请将省电优化设置为无限制(不优化),有利于《短信转发器》保活!</string>
<string name="tips_notification_listener">请先授予《短信转发器》通知使用权否则无法转发APP通知已经自动关闭转发!</string>
<string name="notification_listener_service_enabled">通知服务已开启</string>
<string name="notification_listener_service_disabled">通知服务未开启</string>
</resources>

Loading…
Cancel
Save