編輯:初級開發
目的:使用模擬器互發短信息,比如 5554 和 5556互發短息
關鍵代碼:
發送短信代碼:
Java代碼
package cc.androidos.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
*
* @param demo
* :its own object.
* @param telNum
* : telephone number to send to .
* @param messageText
* : the message need to send .
* @param sendBtn
* :Button.
*
*/
public class SMSDemo extends Activity {
private static SMSDemo demo;
private EditText telNum;
private EditText messageText;
private Button sendBtn;
/**
* override the method onCreate() 1.bind the data telNum and messageText.
* 2.set the sendBtn's listener.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.send);
demo = this;
telNum = (EditText) findVIEwById(R.id.telNumText_send);
messageText = (EditText) findVIEwById(R.id.message_copntent_send);
sendBtn = (Button) findVIEwById(R.id.send_button_send);
/**
* 1.get the data from the input line.. 2.call the SmsManager and send
* the message.
*/
sendBtn.setOnClickListener(new OnClickListener() {
public void onClick(VIEw v) {
String telNumStr = telNum.getText().toString();
String messageStr = messageText.getText().toString();
if (null != telNumStr && null != messageStr) {
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("cc.androidos.smsdemo.IGNORE_ME");
PendingIntent dummyEvent = PendingIntent.getBroadcast(
SMSDemo.this, 0, i, 0);
try {
smsMgr.sendTextMessage(telNumStr, null, messageStr,
dummyEvent, dummyEvent);
} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
} else {
showDialog(getTaskId());
}
}
});
}
/**
* get its object.
*
* @return SMSDemo
*/
public static SMSDemo getApp() {
return demo;
}
}
package cc.androidos.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
*
* @param demo
* :its own object.
* @param telNum
* : telephone number to send to .
* @param messageText
* : the message need to send .
* @param sendBtn
* :Button.
*
*/
public class SMSDemo extends Activity {
private static SMSDemo demo;
private EditText telNum;
private EditText messageText;
private Button sendBtn;
/**
* override the method onCreate() 1.bind the data telNum and messageText.
* 2.set the sendBtn's listener.
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.send);
demo = this;
telNum = (EditText) findVIEwById(R.id.telNumText_send);
messageText = (EditText) findVIEwById(R.id.message_copntent_send);
sendBtn = (Button) findVIEwById(R.id.send_button_send);
/**
* 1.get the data from the input line.. 2.call the SmsManager and send
* the message.
*/
sendBtn.setOnClickListener(new OnClickListener() {
public void onClick(VIEw v) {
String telNumStr = telNum.getText().toString();
String messageStr = messageText.getText().toString();
if (null != telNumStr && null != messageStr) {
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("cc.androidos.smsdemo.IGNORE_ME");
PendingIntent dummyEvent = PendingIntent.getBroadcast(
SMSDemo.this, 0, i, 0);
try {
smsMgr.sendTextMessage(telNumStr, null, messageStr,
dummyEvent, dummyEvent);
} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
} else {
showDialog(getTaskId());
}
}
});
}
/**
* get its object.
*
* @return SMSDemo
*/
public static SMSDemo getApp() {
return demo;
}
}
接受短信的Receiver:
Java代碼
package cc.androidos.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SMSDemo demo = SMSDemo.getApp();
Bundle bundle = intent.getExtras();
StringBuffer sb = new StringBuffer(5000);
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
String address = smsMessage[n].getOriginatingAddress();
sb.append(address + ":").append(smsMessage[n].getMessageBody());
}
Log.e("message", sb.toString());
/**
* call an intent to start the activity DisplayMessage with variable
* message data.
*/
Intent i = new Intent(context, DisplayMessage.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", sb.toString());
context.startActivity(i);
}
}
package cc.androidos.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SMSDemo demo = SMSDemo.getApp();
Bundle bundle = intent.getExtras();
StringBuffer sb = new StringBuffer(5000);
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
String address = smsMessage[n].getOriginatingAddress();
sb.append(address + ":").append(smsMessage[n].getMessageBody());
}
Log.e("message", sb.toString());
/**
* call an intent to start the activity DisplayMessage with variable
* message data.
*/
Intent i = new Intent(context, DisplayMessage.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("message", sb.toString());
context.startActivity(i);
}
}
顯示短信的Activity:
Java代碼
package cc.androidos.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.Net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class DisplayMessage extends Activity {
private EditText messageText;
private EditText messageReplyText;
private Intent intent;
private String telNum;
private String message;
private Button replyBtn;
private ProgressBar bar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.receiver);
messageText = (EditText) findVIEwById(R.id.message_get);
messageReplyText = (EditText) findVIEwById(R.id.message_reply);
replyBtn = (Button) findVIEwById(R.id.reply_button);
bar = (ProgressBar) findVIEwById(R.id.bar);
bar.setVisibility(ProgressBar.INVISIBLE);
intent = getIntent();
String data = intent.getStringExtra("message");
String[] str = data.split(":");
if (str.length == 2) {
telNum = str[0];
message = str[1];
}
messageText.append(data);
messageText.append(" ");
// messageText.append(" ");
replyBtn.setOnClickListener(new OnClickListener() {
public void onClick(VIEw v) {
bar.setVisibility(ProgressBar.VISIBLE);
String replyMessage = messageReplyText.getText().toString();
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("cc.androidos.smsdemo.IGNORE_ME");
PendingIntent dummyEvent = PendingIntent.getBroadcast(
DisplayMessage.this, 0, i, 0);
try {
smsMgr.sendTextMessage(telNum, null, replyMessage,
dummyEvent, dummyEvent);
messageText.append(replyMessage);
messageText.append(" ");
messageReplyText.setText("");
} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
bar.setVisibility(ProgressBar.INVISIBLE);
}
});
}
/**
* get the name by his tel number.
*
* @param telNum
* @return
*/
private String getNameByIdTelnum(String telNum) {
Cursor mCursor = this.getContentResolver().query(Uri.parse("telNum"),
null, null, null, null);
String result = mCursor.getColumnName(1);
Log.e("test name", result);
return "";
// mCursor.getColumnIndex("Sun");
}
}
package cc.androidos.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.Cursor;
import android.Net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class DisplayMessage extends Activity {
private EditText messageText;
private EditText messageReplyText;
private Intent intent;
private String telNum;
private String message;
private Button replyBtn;
private ProgressBar bar;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.receiver);
messageText = (EditText) findVIEwById(R.id.message_get);
messageReplyText = (EditText) findVIEwById(R.id.message_reply);
replyBtn = (Button) findVIEwById(R.id.reply_button);
bar = (ProgressBar) findVIEwById(R.id.bar);
bar.setVisibility(ProgressBar.INVISIBLE);
intent = getIntent();
String data = intent.getStringExtra("message");
String[] str = data.split(":");
if (str.length == 2) {
telNum = str[0];
message = str[1];
}
messageText.append(data);
messageText.append(" ");
// messageText.append(" ");
replyBtn.setOnClickListener(new OnClickListener() {
public void onClick(VIEw v) {
bar.setVisibility(ProgressBar.VISIBLE);
String replyMessage = messageReplyText.getText().toString();
SmsManager smsMgr = SmsManager.getDefault();
Intent i = new Intent("cc.androidos.smsdemo.IGNORE_ME");
PendingIntent dummyEvent = PendingIntent.getBroadcast(
DisplayMessage.this, 0, i, 0);
try {
smsMgr.sendTextMessage(telNum, null, replyMessage,
dummyEvent, dummyEvent);
messageText.append(replyMessage);
messageText.append(" ");
messageReplyText.setText("");
} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
bar.setVisibility(ProgressBar.INVISIBLE);
}
});
}
/**
* get the name by his tel number.
*
* @param telNum
* @return
*/
private String getNameByIdTelnum(String telNum) {
Cursor mCursor = this.getContentResolver().query(Uri.parse("telNum"),
null, null, null, null);
String result = mCursor.getColumnName(1);
Log.e("test name", result);
return "";
// mCursor.getColumnIndex("Sun");
}
}
最後別忘記加入權限:
XML代碼
< uses-permission android:name="android.permission.RECEIVE_SMS" />
< uses-permission android:name="android.permission.SEND_SMS" />
< uses-permission android:name="android.permission.RECEIVE_SMS" />
< uses-permission android:name="android.permission.SEND_SMS" />
測試的時候開兩個模擬器,號碼填入對方模擬器號即可
外特性空間的Activity 我們先來看看,Android應用開發人員接觸的外特性空間中的Activity,對於AMS (activ
ps:對於軟件開發人員來說,保護代碼安全也是比較重要的因素之一,不過目前來說Google Android平台選擇了Java Dalvik VM的方式使其程序很容易破解和
本文收集10款對開發者有用的Android應用,希望能對你的開發有所幫助。如果你還知道有其他對開發者很有用的android應用?也分享出來吧。列表如下:1.Remote