如果收到来自静态号码的短信,我有一个启动活动的应用程序。该活动应惊动我们的消防员。
我已经测试了3种启动活动的方法,但没有一种方法能100%准确地运行。对于大约80%的活动已开始,但有时什么也没有发生。电池优化也被禁用。我也了解了允许的进程,如果该进程处于打ze模式(FCM,SMS等),则可以从后台启动该应用程序,但是现在我找不到链接...清单中SMSReceiver的优先级也999。
可以肯定的是,我已经创建了一个新的应用程序,并再次尝试了三种方式,每次调用Activity / Service / Receiver时,我都会将sms消息写入文件中。有时,短信不会写入文件,因此无法正常工作。
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.falke.smsalarmtester">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SMSReceiver"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<receiver
android:name=".EinsatzReceiver"
android:enabled="true"
android:exported="true" />
<service
android:name=".EinsatzService"
android:enabled="true"
android:exported="true" />
<activity android:name=".EinsatzActivity"></activity>
</application>
</manifest>
1。方式-调用活动:
SMSReceiver:
public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
@Deprecated
public void onReceive(final Context context, Intent intent) {
String MSG_TYPE = intent.getAction();
if (MSG_TYPE != null && MSG_TYPE.equals("android.provider.Telephony.SMS_RECEIVED")) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
if (pdusObj != null) {
for (Object aPdusObj : pdusObj) {
SmsMessage currentMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj, format);
} else {
//depreciated in API level 23
currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
}
String senderNum = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
Intent i = new Intent(context, EinsatzService.class);
//Intent i = new Intent(context, EinsatzActivity.class);
//Intent i = new Intent(context, EinsatzReceiver.class);
i.putExtra("message", message);
i.putExtra("number", senderNum);
context.startService(i);
//context.startActivity(i);
//setAlarm(context,1,i,System.currentTimeMillis());
}
}
}
} catch (Exception e) {
Log.e(TAG, "Exception smsReceiver" + e);
e.printStackTrace();
}
}
}
}
EinsatzActivity:
public class EinsatzActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if(intent != null) {
String body = intent.getStringExtra("message");
String number = intent.getStringExtra("number");
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.getDefault());
String dateString = formatter.format(new Date(c.getTimeInMillis()));
String data = dateString + ";" + number + ";" + body + "\n";
writeToFile(data, getApplicationContext());
}
}
}
2。方式-致电服务:
我在SMSReceiver中将startActivity替换为startService,并尝试使用Service。
EinsatzService:
public class EinsatzService extends Service {
public EinsatzService() {}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null) {
String body = intent.getStringExtra("message");
String number = intent.getStringExtra("number");
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.getDefault());
String dateString = formatter.format(new Date(c.getTimeInMillis()));
String data = dateString + ";" + number + ";" + body + "\n";
writeToFile(data, getApplicationContext());
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
3。方式-调用BroadcastReceiver:
然后我尝试使用BroadcastReceiver,但也无法正常工作。
我在SMSReceiver中删除了startService,并添加了一个新功能setAlarm。
setAlarm(context,1,i,System.currentTimeMillis());
功能:
public static void setAlarm(Context context, int id, Intent intent, long time) {
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, 0);
if (mgrAlarm != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mgrAlarm.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
mgrAlarm.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else {
mgrAlarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
}
}
EinsatzReceiver:
public class EinsatzReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null) {
String body = intent.getStringExtra("message");
String number = intent.getStringExtra("number");
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss", Locale.getDefault());
String dateString = formatter.format(new Date(c.getTimeInMillis()));
String data = dateString + ";" + number + ";" + body + "\n";
writeToFile(data, getApplicationContext());
}
}
}
大多数情况下,每种方式都会检测到短信,但有时并非如此。我需要每次都启动警报,以替代寻呼机。
感谢您的帮助!