package ru.blagin.xmppsmsgate; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; public class XMPPSMSGateActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage("5556",null,"Text SMS",null,null); } }
destinationAddress - the number to which the message is sent;
scAddress - The number of the SMS center of your cellular operator through which the message is sent, if this parameter has a zero value, then the default number is used;
text - SMS text message;
sentIntent - If not a null value, then a PendingIntent object is sent to this parameter to receive messages about the result of sending the message;
deliveryIntent - If not a null value, then a PendingIntent object is passed to this parameter to receive messages about the result of the message delivery.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="top|left"/> </LinearLayout>
package ru.blagin.xmppsmsgate; import java.util.ArrayList; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.SmsManager; import android.widget.TextView; public class XMPPSMSGateActivity extends Activity { TextView tv = null; String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; private BroadcastReceiver sent = null; private BroadcastReceiver delivered = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // TextView tv = (TextView) findViewById(R.id.textView); // : IntentFilter in_sent = new IntentFilter(SENT); sent = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { tv.append(intent.getStringExtra("PARTS")+": "); tv.append(intent.getStringExtra("MSG")+": "); switch(getResultCode()) { case Activity.RESULT_OK: tv.append("SMS \n"); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: tv.append(" \n"); break; case SmsManager.RESULT_ERROR_NO_SERVICE: tv.append(" \n"); break; case SmsManager.RESULT_ERROR_NULL_PDU: tv.append("Null PDU\n"); break; case SmsManager.RESULT_ERROR_RADIO_OFF: tv.append(" \n"); break; } } }; registerReceiver(sent, in_sent); // : IntentFilter in_delivered = new IntentFilter(DELIVERED); delivered = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { tv.append(intent.getStringExtra("PARTS")+": "); tv.append(intent.getStringExtra("MSG")+": "); switch (getResultCode()) { case Activity.RESULT_OK: tv.append("SMS \n"); break; case Activity.RESULT_CANCELED: tv.append("SMS \n"); break; } } }; registerReceiver(delivered, in_delivered); SendSMS("_"," > 160 ."); } // SMS public void SendSMS(String phone, String message) { SmsManager sms = SmsManager.getDefault(); ArrayList<String> al_message = new ArrayList<String>(); al_message = sms.divideMessage(message); ArrayList<PendingIntent> al_piSent = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> al_piDelivered = new ArrayList<PendingIntent>(); for (int i = 0; i < al_message.size(); i++) { Intent sentIntent = new Intent(SENT); sentIntent.putExtra("PARTS", ": "+i); sentIntent.putExtra("MSG", ": "+al_message.get(i)); PendingIntent pi_sent = PendingIntent.getBroadcast(this, i, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT); al_piSent.add(pi_sent); Intent deliveredIntent = new Intent(DELIVERED); deliveredIntent.putExtra("PARTS", ": "+i); deliveredIntent.putExtra("MSG", ": "+al_message.get(i)); PendingIntent pi_delivered = PendingIntent.getBroadcast(this, i, deliveredIntent, PendingIntent.FLAG_UPDATE_CURRENT); al_piDelivered.add(pi_delivered); } sms.sendMultipartTextMessage(phone, null, al_message, al_piSent, al_piDelivered); } @Override protected void onDestroy() { if(sent != null) unregisterReceiver(sent); if(delivered != null) unregisterReceiver(delivered); super.onDestroy(); } }
package ru.blagin.xmppsmsgate; import java.util.Collection; import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.SASLAuthentication; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.filter.AndFilter; import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Presence; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class XMPPSMSGateService extends Service { private ConnectionConfiguration connConfig; private XMPPConnection connection; Thread th = null; Intent in = new Intent("SMSGate_Service"); @Override public IBinder onBind(Intent arg0){return null;} @Override public int onStartCommand(Intent intent, int flags, int startId){return Service.START_STICKY;} @Override public void onCreate() { super.onCreate(); th= new Thread() { public void run() { in.putExtra("Message","The service is started"); sendBroadcast(in); connConfig = new ConnectionConfiguration(/*domen*/,5222,/*server*/); SASLAuthentication.supportSASLMechanism("PLAIN"); connConfig.setCompressionEnabled(false); connConfig.setSASLAuthenticationEnabled(true); connection = new XMPPConnection(connConfig); try { in.putExtra("Message","Connect to the XMPP server"); sendBroadcast(in); connection.connect(); in.putExtra("Message","Login into the XMPP server"); sendBroadcast(in); connection.login(/*login*/,/*password*/); if(connection.isConnected()) { in.putExtra("Message","SMS Gate online."); sendBroadcast(in); }else { in.putExtra("Message","SMS Gate offline."); sendBroadcast(in); } Presence presence = new Presence(Presence.Type.available); presence.setStatus("SMS Gate"); presence.setPriority(30); connection.sendPacket(presence); PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class)); PacketListener myListener = new PacketListener() { public void processPacket(Packet packet) { if(packet instanceof Message) { Message message = (Message) packet; String messageBody = message.getBody(); String JID = message.getFrom(); if(messageBody == null) { messageBody = ""; Collection<Message.Body> bodies = message.getBodies(); for(Message.Body r:bodies){messageBody += r.getMessage();} } if(messageBody.equals("ping")){sendMessage(JID,"pong");} in.putExtra("Message",messageBody); sendBroadcast(in); } } }; connection.addPacketListener(myListener, filter); while(connection.isConnected()) { try{Thread.sleep(1000);}catch(Exception e){Log.e(this.getClass().getName(),e.getMessage());} } }catch(Exception e) { Log.e(this.getClass().getName(),e.getMessage()); in.putExtra("Message","ERROR: "+e.getMessage()); sendBroadcast(in); } } public void sendMessage(String to, String message) { if(!message.equals("")) { ChatManager chatmanager = connection.getChatManager(); Chat newChat = chatmanager.createChat(to, null); try{newChat.sendMessage(message);} catch(Exception e) {Log.e(this.getClass().getName(),e.getMessage());} } } }; th.start(); } @Override public void onDestroy() { if(connection.isConnected()){connection.disconnect();th = null;} in.putExtra("Message","The service is stopped"); sendBroadcast(in); } }
IntentFilter filter = new IntentFilter(); filter.addAction("SMSGate_Service"); service = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("SMSGate_Service")) { String message = intent.getStringExtra("Message"); tv.append(message+"\n"); int i = message.indexOf("@"); if(i != -1) { String phone = message.substring(0, i); if(phone.length() != 0) { String text = message.substring(i+1,message.length()); tv.append("Sending SMS...\n"); SendSMS(phone,text); }else{/*phone: 0*/} }else{/*not sms message*/} } } }; registerReceiver(service, filter);
5556@
startService(new Intent(this,XMPPSMSGateService.class));
stopService(new Intent(this,XMPPSMSGateService.class));
Source: https://habr.com/ru/post/135627/
All Articles