Android開發筆記——PendingIntent

來源:互聯網
上載者:User

package com.mobile.Main;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.PendingIntent;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.IntentFilter;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.Contacts.People;

import android.telephony.gsm.SmsManager;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button; import android.widget.EditText;

import android.widget.ImageButton;

import android.widget.Toast;

public class Ex12_3_2 extends Activity

{

/**按鈕控制項*/ private Button system_sms_button;

private Button customize_sms_button;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

system_sms_button = (Button)this.findViewById(R.id.main_button1);

system_sms_button.setOnClickListener(new OnClickListener()

{ public void onClick(View v) { sendSms("123456789","系統簡訊功能"); } });

customize_sms_button = (Button)this.findViewById(R.id.main_button2);

customize_sms_button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { sendSMS("",""); } }); }

/** * 調用系統的傳送簡訊

* @param address 地址

* @param body 資訊內容 */

private void sendSms(String address,String body)

{ try { Intent intent = new Intent(Intent.ACTION_VIEW);

intent.putExtra("address", address);

intent.putExtra("sms_body",body);

intent.setType("vnd.android-dir/mms-sms");

startActivity(intent); } catch (Exception e) { e.printStackTrace(); } }

/**對話方塊執行個體 */

private AlertDialog sendSMSDialog;

/** 自訂ACTION常數,作為廣播的Intent Filter識別常數 */

private String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN";

private String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";

/** 建立兩個廣播對象*/ private MyBroadcastReceiver myBroadcastReceiver1;

private MyBroadcastReceiver myBroadcastReceiver2;

/**選取連絡人返回後的狀態代碼*/

private final int backCode = 1;

/** * 推薦好友 */

private void sendSMS(final String content1, final String content2)

{ LayoutInflater factory = LayoutInflater.from(this);

final View textEntryView = factory.inflate(R.layout.showdialog_smsentry_layout, null);

final EditText editText_phone = (EditText) textEntryView.findViewById(R.id.showdialog_smsentry_edittext_phone);

editText_phone.setText(content1);

final EditText editText_content = (EditText) textEntryView.findViewById(R.id.showdialog_smsentry_edittext_content);

editText_content.setText(content2);

final ImageButton imageButton = (ImageButton) textEntryView.findViewById(R.id.showdialog_smsentry_imagebutton); imageButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { //跳轉到系統連絡人清單 Uri uri = Uri.parse("content://contacts/people"); Intent intent = new Intent(Intent.ACTION_PICK, uri);

startActivityForResult(intent, backCode);

sendSMSDialog.dismiss(); } });

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(android.R.drawable.ic_dialog_info);

builder.setTitle(getString(R.string.title));

builder.setView(textEntryView);

builder.setPositiveButton(getString(R.string.certain), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String number = editText_phone.getText().toString();

String content = editText_content.getText().toString();

if (number == null || number.length() == 0)

{

//判斷電話是否為空白 Toast.makeText(Ex12_3_2.this, "電話號碼不可為空!", Toast.LENGTH_LONG).show();

sendSMS(number, content); }

else {

// 自訂IntentFilter為SENT_SMS_ACTIOIN Receiver IntentFilter mFilter01;

mFilter01 = new IntentFilter(SMS_SEND_ACTIOIN);

myBroadcastReceiver1 = new MyBroadcastReceiver();

//開啟廣播 registerReceiver(myBroadcastReceiver1, mFilter01);

// 自訂

IntentFilter為DELIVERED_SMS_ACTION Receiver mFilter01 = new IntentFilter(SMS_DELIVERED_ACTION);

myBroadcastReceiver2 = new MyBroadcastReceiver();

//開啟廣播 registerReceiver(myBroadcastReceiver2, mFilter01);

try { // 建立SmsManager對象 SmsManager smsManager = SmsManager.getDefault();

// 建立自訂Action常數的Intent(給PendingIntent參數之用)

Intent itSend = new Intent(SMS_SEND_ACTIOIN);

Intent itDeliver = new Intent(SMS_DELIVERED_ACTION); // sentIntent參數為傳送後接受的廣播資訊

PendingIntent PendingIntent sendPI = PendingIntent .getBroadcast(getApplicationContext(),0, itSend, 0);

// deliveryIntent參數為送達後接受的廣播資訊

PendingIntent PendingIntent deliverPI = PendingIntent .getBroadcast(getApplicationContext(),0, itDeliver, 0);

// 發送SMS簡訊,注意倒數的兩個PendingIntent參數

smsManager.sendTextMessage(number,null,content,sendPI, deliverPI);

} catch (Exception e) { e.printStackTrace(); } } } });

builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener()

{ public void onClick(DialogInterface dialog, int whichButton) { } });

sendSMSDialog = builder.create(); sendSMSDialog.show(); sendSMSDialog.setCanceledOnTouchOutside(true);

}

/** * 聆聽簡訊狀態 */

public class MyBroadcastReceiver extends BroadcastReceiver

{ @Override public void onReceive(Context context, Intent intent)

{ try { String action = intent.getAction();

if(action.equals(SMS_SEND_ACTIOIN)){ }else if(action.equals(SMS_DELIVERED_ACTION)){ }

if (getResultCode() == Activity.RESULT_OK) { Toast.makeText(Ex12_3_2.this, "發送成功!", Toast.LENGTH_LONG).show();

} else { Toast.makeText(Ex12_3_2.this, "發送失敗!", Toast.LENGTH_LONG).show(); }

//登出廣播

unregisterReceiver(myBroadcastReceiver1);

unregisterReceiver(myBroadcastReceiver2); }

catch (Exception e) { e.getStackTrace(); } } }

/** * 接收連絡人返回資料 */

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode)

{

case backCode: if (data != null)

{ final Uri uriRet = data.getData();

if (uriRet != null) {

try { Cursor c = managedQuery(uriRet, null, null, null, null);

c.moveToFirst();

/* 抓取通訊錄的姓名 */

String strName = c.getString(c.getColumnIndexOrThrow(People.NAME));

/* 抓取通訊錄的電話 */

String strPhone = c.getString(c.getColumnIndexOrThrow(People.NUMBER));

if (strName != null) { Toast.makeText(Ex12_3_2.this, strName, Toast.LENGTH_LONG).show(); }

else { Toast.makeText(Ex12_3_2.this, "連絡人為空白!", Toast.LENGTH_LONG).show(); }

sendSMS(strPhone,""); } catch (Exception e) { e.printStackTrace(); sendSMS("","");

Toast.makeText(Ex12_3_2.this, "錯誤!", Toast.LENGTH_LONG).show();

} } } else { sendSMS("","");

Toast.makeText(Ex12_3_2.this, "連絡人為空白!", Toast.LENGTH_LONG).show(); } break;

}

super.onActivityResult(requestCode, resultCode, data); } }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.