问题描述
我在我的默认 SMS 应用程序中使用前台服务,但我注意到其他应用程序不使用该服务,但即使应用程序未打开仍然可以获取交付报告,想知道这些应用程序是如何工作的。
我正在使用下面的代码来接收发送和交付报告,非常欢迎任何见解!
我一直在拼命寻找答案,找了很长时间,但没有得到解决方案。
public class SendMySMS extends Service {
int count = 0;
public static final String PRIMARY_NOTIF_CHANNEL = "default";
public static final int PRIMARY_FOREGROUND_NOTIF_SERVICE_ID = 1009;
broadcastReceiver sendReceiver;
broadcastReceiver deliveryReceiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent,int flags,int startId) {
super.onStartCommand(intent,flags,startId);
Log.d(TAG4,"SendMySMS service started");
if (intent!=null) {
String str_number = intent.getStringExtra("KEY_number");
String str_body = intent.getStringExtra("KEY_body");
String str_subId = intent.getStringExtra("KEY_sub_id");
if (str_number!=null && str_body!=null && str_subId!=null && !str_number.equals("") && !str_body.equals("") && !str_subId.equals("")) {
count++;
sendSMS(str_number,str_body,str_subId);
}else{
Toast.makeText(getBaseContext(),"Could't send sms",Toast.LENGTH_LONG).show();
}
}
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel chan1 = new NotificationChannel(PRIMARY_NOTIF_CHANNEL,"Background",notificationmanager.IMPORTANCE_NONE);
chan1.setLightColor(Color.TRANSPARENT);
chan1.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
notificationmanager.createNotificationChannel(chan1);
Intent intent1 = new Intent(this,MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this,PRIMARY_NOTIF_CHANNEL)
.setSmallIcon(R.drawable.ic_sms_notif)
.setContentTitle("Waiting for delivery report..")
.setPriority(NotificationCompat.PRIORITY_MIN)
.setAutoCancel(true)
.build();
startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID,notification);
}
}
@Override
public void onDestroy() {
try {
unregisterReceiver(sendReceiver);
unregisterReceiver(deliveryReceiver);
} catch (Exception e) {
e.printstacktrace();
Log.d(TAG4,"receiver not unregistered onDestroy");
}
}
public void sendSMS(final String mobNo,String message,String subId) {
sendReceiver = new SendReceiver();
deliveryReceiver = new DeliveryReceiver();
long tsLong = System.currentTimeMillis();
String ts = Long.toString(tsLong);
ContentValues values = new ContentValues();
values.put("address",mobNo);
values.put("body",message);
values.put("sub_id",subId);
values.put("date_sent",ts);
values.put("status","-1");
values.put("error_code","2");
getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"),values);
String msgid="";
String[] reqCols = new String[] {"_id","address","date_sent"};
Uri urimsg = Uri.parse(String.valueOf(Telephony.Sms.CONTENT_URI));
ContentResolver cr = this.getContentResolver();
Cursor c = cr.query(urimsg,reqCols,null,null);
assert c != null;
int totalSMS = Math.min(c.getCount(),10);
if (c.movetoFirst()) {
for (int i=0; i<totalSMS; i++) {
if (c.getString(c.getColumnIndexOrThrow("date_sent")).equals(ts) && c.getString(c.getColumnIndexOrThrow("address")).equals(mobNo)) {
msgid = c.getString(0);
break;
}else{
msgid="not_found";
}
c.movetoNext();
}
}
c.close();
ArrayList<String> parts = SmsManager.getDefault().divideMessage(message);
int numParts = parts.size();
ArrayList<PendingIntent> sentPIs = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPIs = new ArrayList<PendingIntent>();
String smsSent = "SMS_SENT"+msgid;
String smsDelivered = "SMS_DELIVERED"+msgid;
Intent sentIntent = new Intent(smsSent);
sentIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sentIntent.putExtra("key_msg_id",msgid);
sentIntent.putExtra("key_number",mobNo);
PendingIntent sentPI = PendingIntent.getbroadcast(this,sentIntent,PendingIntent.FLAG_ONE_SHOT);
Intent deliveryIntent = new Intent(smsDelivered);
deliveryIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
deliveryIntent.putExtra("key_msg_id",msgid);
deliveryIntent.putExtra("key_number",mobNo);
PendingIntent deliveredPI = PendingIntent.getbroadcast(this,deliveryIntent,PendingIntent.FLAG_ONE_SHOT);
for (int i = 0; i < numParts; i++) {
sentPIs.add(sentPI);
deliveredPIs.add(deliveredPI);
}
registerReceiver(sendReceiver,new IntentFilter(smsSent));
registerReceiver(deliveryReceiver,new IntentFilter(smsDelivered));
if (numParts > 1){
SmsManager.getSmsManagerForSubscriptionId(Integer.parseInt(subId)).sendMultipartTextMessage(mobNo,parts,sentPIs,deliveredPIs);
}else {
SmsManager.getSmsManagerForSubscriptionId(Integer.parseInt(subId)).sendTextMessage(mobNo,message,sentPI,deliveredPI);
}
class SendReceiver extends broadcastReceiver {
@Override
public void onReceive(Context arg0,Intent arg1) {
Bundle bundle = arg1.getExtras();
String msgid1="";
String number="";
if (bundle!=null) {
msgid1 = bundle.getString("key_msg_id");
number = bundle.getString("key_number");
}
switch (getResultCode()) {
case Activity.RESULT_OK://-1
Toast.makeText(getBaseContext(),"SMS sent",Toast.LENGTH_LONG).show();
Log.d(TAG4,"sms sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: //1
Toast.makeText(getBaseContext(),"Generic failure",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE://4
Toast.makeText(getBaseContext(),"No service",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU://3
Toast.makeText(getBaseContext(),"Null PDU",Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_RAdio_OFF://2
Toast.makeText(getBaseContext(),"Radio off",Toast.LENGTH_LONG).show();
break;
}
}
}
}
class DeliveryReceiver extends broadcastReceiver {
@Override
public void onReceive(Context arg0,Intent arg1) {
Bundle bundle = arg1.getExtras();
String msgid1="";
String number="";
if (bundle!=null) {
msgid1 = bundle.getString("key_msg_id");
number = bundle.getString("key_number");
}
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(),"SMS delivered",Toast.LENGTH_LONG).show();
count--;
if (count==0){
stopSelf();
}
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),"SMS not delivered",Toast.LENGTH_LONG).show();
count--;
if (count==0){
stopSelf();
}
break;
default:
Toast.makeText(getBaseContext(),"SMS not delivered (Error-100)",Toast.LENGTH_LONG).show();
count--;
if (count==0){
stopSelf();
}
break;
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)