1.使用Toast
/*使用系统标准的 makeText()方式来产生Toast讯息*/
Toast.makeText( EX04_03.this, "你的愿望 "+Str.toString()+"已送达耶诞老人信箱", Toast.LENGTH_LONG).show();
/*使用带图片的Toast*/
LinearLayout lay = new LinearLayout(EX05_07.this);
/*设定mTextView去抓取string值*/
mTextView.setText(R.string.app_url);
/*用Toast方式显示*/
Toast toast = Toast.makeText(EX05_07.this, mTextView .getText(), Toast.LENGTH_LONG);
View textView = toast.getView();
lay.setOrientation(LinearLayout.HORIZONTAL);
/*在Toast里加上图片*/
mView01.setImageResource(R.drawable.icon);
/*在Toast里显示图片*/ lay.addView(mView01);
/*在Toast里显示文字*/ lay.addView(textView);
toast.setView(lay);
toast.show();
2.拨打电话
/*建构一个新的Intent并执行action.CALL的常数与透过Uri将字符串带入*/
Intent myIntentDial = new Intent("android.intent.action.CALL",Uri.parse("tel:"+strInput));
/*在startActivity()方法中带入自定义的Intent对象以执行拨打电话的工作*/
startActivity(myIntentDial);
3.发短信
/*建构一取得default instance的 SmsManager对象 */
SmsManager smsManager = SmsManager.getDefault();
/* 先建构一PendingIntent对象并使用getBroadcast()方法进行Broadcast *
/ * 将PendingIntent,电话,简讯文字等参数传入sendTextMessage()方法发送简讯*/
PendingIntent mPI = PendingIntent.getBroadcast(EX05_03.this, 0, new Intent(), 0);
smsManager.sendTextMessage(strDestAddress, null, strMessage, mPI, null);
4.发邮件
/*透过Intent来发送邮件*/
Intent mEmailIntent =
new Intent(android.content.Intent.ACTION_SEND);
/*设定邮件格式为plain/text*/
mEmailIntent.setType("plain/text");
/*取得EditText01,02,03,04的值作为收件人地址,附件,主题,内容*/
strEmailReciver = new String[]{mEditText01.getText().toString()};
strEmailCc = new String[]{mEditText02.getText().toString()};
strEmailSubject = mEditText03.getText().toString();
strEmailBody = mEditText04.getText().toString();
/*将取得的字符串放入mEmailIntent中*/
mEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, strEmailReciver);
mEmailIntent.putExtra(android.content.Intent.EXTRA_CC, strEmailCc);
mEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, strEmailSubject);
mEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, strEmailBody);
/*开启Gmail 并将相关参数传入*/
startActivity(Intent.createChooser(mEmailIntent, getResources().getString(R.string.str_message)));
5.使用Notication
/* 建立新的Intent,作为点选Notification留言条时,
* 会执行的Activity */
Intent notifyIntent=new Intent(this,EX05_08_1.class);
notifyIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
/* 建立PendingIntent作为设定递延执行的Activity */
PendingIntent appIntent=PendingIntent.getActivity(EX05_08.this,0,
notifyIntent,0);
/* 建立Notication,并设定相关参数 */
Notification myNoti=new Notification();
/* 设定statusbar显示的icon */
myNoti.icon=iconId;
/* 设定statusbar显示的文字讯息 */
myNoti.tickerText=text;
/* 设定notification发生时同时发出预设声音 */
myNoti.defaults=Notification.DEFAULT_SOUND;
/* 设定Notification留言条的参数 */
myNoti.setLatestEventInfo(EX05_08.this,"MSN登入状态",text,appIntent);
/* 送出Notification */
myNotiManager.notify(0,myNoti);
6.横式直式切换
if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
/* 若当前为横式,则变更为直式显示 */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
/* 若当前为直式,则变更为横式显示 */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}