发送短信 |
android |
|
1 发送短信的方法:
SmsManager smsManager = SmsManager.getDefault();smsManager.sendTextMessage(destAddr, null, mMessageText, il, null);
2.ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
如果你想把短信内容保存在收件箱里面 那么只要吧uri改成:
"content://sms/inbox".
3.
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms://"));
sendIntent.putExtra("address", "123456789");
sendIntent.putExtra("sms_body", "foo bar");
startActivity(sendIntent);
|
发送邮件 |
android |
|
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
File file = new File(Environment.getExternalStorageDirectory().getPath()+ File.separator + “first.txt”);
intent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { “madgoat@qq.com” });
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, “Hello Madgoat”);
intent.putExtra(android.content.Intent.EXTRA_TEXT,“Dear Madgoat:\n\tThis a test mail from android client.\nThanks for you visit http://madgoat.cn !”);
intent.setType(“application/octet-stream”);
//当无法确认发送类型的时候使用如下语句
//intent.setType(“*/*”);
//当没有附件,纯文本发送时使用如下语句
//intent.setType(“plain/text”);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(intent, “Mail Test”));
|
开启、关闭WIFI |
android |
http://www.eoeandroid.com/thread-407-1-10.html |
// 权限:
// android.permission.ACCESS_WIFI_STATE
// android.permission.CHANGE_WIFI_STATE
// android.permission.WAKE_LOCK
WifiManager wificonn= (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wificonn.isWifiEnabled()) {
wificonn.setWifiEnabled(false);
} else {
wificonn.setWifiEnabled(true);
}
|
建立GPRS连接 + 请求root权限 |
android |
|
//没有合适的开关GPRS的方法,只能默认手动打开GPRS,使用svc data enable/disable来开关
//请求root权限,开一个新的shell环境
Process process = Runtime.getRuntime().exec("su");
//方法一
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("svc data " + cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
//方法二,使用隐藏类
public final class DataConManager {
private TelephonyManager m_telManager = null;
private ConnectivityManager m_conManager = null;
public DataConManager(Context context) {
try {
// Get phone and connectivity services
m_telManager = (TelephonyManager) context.getSystemService("phone");
m_conManager = (ConnectivityManager) context.getSystemService("connectivity");
} catch (Exception e) {
m_telManager = null;
m_conManager = null;
}
}
boolean switchState(boolean enable) {
boolean bRes = false;
// Data Connection mode (only if correctly initialized)
if (m_telManager != null) {
try {
// Will be used to invoke hidden methods with reflection
Class cTelMan = null;
Method getITelephony = null;
Object oTelephony = null;
Class cTelephony = null;
Method action = null;
// Get the current object implementing ITelephony interface
cTelMan = m_telManager.getClass();
getITelephony = cTelMan.getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
oTelephony = getITelephony.invoke(m_telManager);
// Call the enableDataConnectivity/disableDataConnectivity
// method
// of Telephony object
cTelephony = oTelephony.getClass();
if (enable) {
action = cTelephony.getMethod("enableDataConnectivity");
} else {
action = cTelephony.getMethod("disableDataConnectivity");
}
action.setAccessible(true);
bRes = (Boolean) action.invoke(oTelephony);
} catch (Exception e) {
bRes = false;
}
}
return bRes;
}
public boolean isEnabled() {
boolean bRes = false;
// Data Connection mode (only if correctly initialized)
if (m_conManager != null) {
try {
// Get Connectivity Service state
NetworkInfo netInfo = m_conManager.getNetworkInfo(0);
// Data is enabled if state is CONNECTED
bRes = (netInfo.getState() == NetworkInfo.State.CONNECTED);
} catch (Exception e) {
bRes = false;
}
}
return bRes;
}
}
|
文件读写 |
android |
|
//读:
public String ReadSettings(Context context){
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = openFileInput("settings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
//写:
public void WriteSettings(Context context, String data){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = openFileOutput("settings.dat",MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
获取手机SIM卡信息 |
android |
|
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);//
//
//IMSI:international mobiles subscriber identity国际移动用户号码标识,
//MSISDN:mobile subscriber ISDN用户号码,这个是我们说的139,136那个号码;
//ICCID:ICC identity集成电路卡标识,这个是唯一标识一张卡片物理号码的;
//IMEI:international mobile Equipment identity手机唯一标识码;
//
String str = "";
str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n"; //全球手机唯一码
str += "Line1Number = " + tm.getLine1Number() + "\n"; //若不为空则为本机号码,MSISDN
str += "NetworkOperator = " + tm.getNetworkOperator() + "\n"; //以下为运营商
str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n"; //手机串号,ICCID
str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
|