public class MainActivity extends Activity {
private EditText et_number;
private EditText et_content;
private SharedPreferences pdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdf = this.getSharedPreferences("smsinfo", MODE_PRIVATE);
et_number = (EditText) this.findViewById(R.id.et_number);
et_content = (EditText) this.findViewById(R.id.et_content);
// 将数据回显过来
String number = pdf.getString("number", "");
String content = pdf.getString("content", "");
et_number.setText(number);
et_content.setText(content);
}
/**
* 当该Activity被销毁的时候,进数据保存到SharedPreferences
*/
@Override
protected void onDestroy() {
super.onDestroy();
String number = et_number.getText().toString();
String content = et_content.getText().toString();
Editor edit = pdf.edit();
edit.putString("number", number);
edit.putString("content", content);
edit.commit();
}
/**
* 发送短信
*
* @param view
*/
public void sendSms(View view) {
String number = et_number.getText().toString();
String content = et_content.getText().toString();
if (TextUtils.isEmpty(number) || TextUtils.isEmpty(content)) {
Toast.makeText(this, "号码或短信内容不能为空!", 0).show();
return;
}
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> divideMessage = smsManager.divideMessage(content);
for (String message : divideMessage) {
smsManager.sendTextMessage(number, null, message, null, null);
}
Toast.makeText(this, "短信发送成功!", 0).show();
}
} 结果:
public class MainActivity extends Activity {
private SharedPreferences pdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdf = this.getSharedPreferences("videoinfo", MODE_PRIVATE);
}
/**
* 当界面可见时,将视频恢复,从记录的位置播放。
*/
@Override
protected void onStart() {
super.onStart();
int progress = pdf.getInt("videoProgress", 0);
if (progress > 0) {
Toast.makeText(this, "视频恢复播放,从" + progress + "位置开始播放", 0).show();
}
}
/**
* 当界面不可见时,比如电话打进来,将视频暂停,并记录当前播放位置。
*/
@Override
protected void onStop() {
super.onStop();
int position = 1000;
Toast.makeText(this, "视频暂停播放了,位置为:" + position, 0).show();
Editor edit = pdf.edit();
edit.putInt("videoProgress", position);// 保存当前视频播放的位置
edit.commit();
}
}