Context context = getApplicationContext();
CharSequence text = "have a toast thank you ,you've got it!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
發送短信相當的簡單,只需要幾行代碼,如下:
import android.telephony.gsm.SmsManager;
import android.app.PendingIntent;
......
SmsManager sms = SmsManager.getDefault();
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
sms.sendTextMessage(phoneNumber, null, MsgStr, pi, null);
其中參數phoneNumber和MsgStr均是String類型,表示接收方的電話號碼和短信內容
接收短信相對而言麻煩一些,需要使用全局的接收類BroadcastReceiver,完整代碼如下:
package com.hello.db;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class MessageDemo extends BroadcastReceiver {
private static final String strACT = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(strACT)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msg = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage currMsg : msg) {
sb.append("From:");
sb.append(currMsg.getDisplayOriginatingAddress());
sb.append("\nMessage:");
sb.append(currMsg.getDisplayMessageBody());
}
}
Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
Intent i = new Intent(context, DBDemo.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
此處的接收是收到消息時,彈出一個Toast提示,在此基礎上稍做修改也可以實現將收到的消息放入文本框從而展示出來。注意最后必須將控制權交還給上一個Activity,不然程序會出錯。
好了,收發短信的代碼都有了,下面需要配置權限,只有程序擁有了權限,這些功能才能真正被使用
修改AndroidManifest.xml,在Activity下添加receiver節點:
<receiver android:name="MessageDemo">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
隨后在application下添加節點:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
abd shell
cd data/data
ls
cd databases
ls
sqlite2 ***.db
.table
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:XML id="xmlFile" source="menu.xml"/>
<mx:Label text="人員名單" fontSize="18" x="595" y="70"/>
<!-- 定義grid:指定數據來源,表頭-->
<mx:DataGrid dataProvider="{xmlFile.order}" width="468" x="419" y="118" height="258">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="姓名" fontSize="14"/>
<mx:DataGridColumn dataField="rest" headerText="餐館名" fontSize="14"/>
<mx:DataGridColumn dataField="food" headerText="菜名" fontSize="14"/>
<mx:DataGridColumn dataField="time" headerText="時間" fontSize="14"/>
<mx:DataGridColumn dataField="ip" headerText="IP" fontSize="14" width="115"/>
<mx:DataGridColumn dataField="price" headerText="價錢" fontSize="14"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
function selected(rtn:RadioButton):void{
Alert.show(""+rtn);
}
]]>
</mx:Script>
<mx:Button label="Check Answer"
click="Alert.show(option1.selected?'Correct Answer!':'Wrong Answer', 'Result')" x="430" y="117"/>
<mx:RadioButton groupName="year" id="option1" label="1942" value="2" x="500" y="49" click="selected(option1)" />
<mx:RadioButton groupName="year" id="option2" label="1952" value="1" x="430" y="49" click="selected(option2)"/>
</mx:Application>
今天很高興開了這個博客
這個博客地址是在百度上搜索經常遇到
所以就進來了
感覺不錯
非常不錯