Android - sending SMS in a separate stream

I am writing an Android application (with a service) where I need to send a message when I receive a GCM notification.

The main problem: when I send 3 SMS at the same time, any other operation stops for a long time (10-20 seconds), and the phone is also blocked. I tried to send an SMS to the stream, but there are no differences.

SmsSender.java

public class SmsSender implements Runnable{ private Context context; private String phoneNumber; private String message; private int id; public SmsSender(Context paramContext, String phoneNumber, String message, int id) { super(); this.context = paramContext; this.phoneNumber = phoneNumber; this.message = message; this.id = id; } public void run() { try { Intent sentIntent = new Intent("sent"); Intent deliveredIntent = new Intent("delivered"); sentIntent.putExtra("id", id); deliveredIntent.putExtra("id", id); PendingIntent sentPI = PendingIntent.getBroadcast(this.context, id,sentIntent, 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this.context, id, deliveredIntent, 0); SmsManager smsManager = SmsManager.getDefault(); ArrayList<String> parts = smsManager.divideMessage(message); ArrayList<PendingIntent> sentList = new ArrayList<PendingIntent>(); sentList.add(sentPI); ArrayList<PendingIntent> deliveredList = new ArrayList<PendingIntent>(); deliveredList.add(deliveredPI); smsManager.sendMultipartTextMessage(phoneNumber, null, parts, sentList, deliveredList); } catch (Exception e) { e.printStackTrace(); } } } 

Start thread

new topic (new SmsSender (getApplicationContext (), phone number, message, id)). start ();

Where is the problem? Why is SMS sent in the main stream and not in my stream?

+4
source share
1 answer

According to android documentation

What is a service? The big confusion about the Service class actually revolves around the fact that it is not: Service is not a separate process. The service object itself does not imply that it works in its own process; Unless otherwise specified, it runs in the same process as the application in which it is included.
Service is not a thread. This does not mean that you need to do the work of the main thread (to avoid errors associated with application errors).

0
source

Source: https://habr.com/ru/post/1440037/


All Articles