1、對應(yīng)一個接口即可,比如OnMsgChangeListener,代碼:
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比華池網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式華池網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋華池地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。
public?interface?OnMsgChangeListener?{
public?void?doMsg(MsgBean?bean);
}
2、定義MsgBeanActivity處理MsgBean數(shù)據(jù)
3、定義MsgBean實體
4、在MsgBeanActivity定義setMsgChangeListener()方法,傳入的OnMsgChangeListener接口對象,如下代碼:
setMsgChangeListener(OnMsgChangeListener?onMsg)
5、重寫接口方法
在Android平臺調(diào)用Web Service需要依賴于第三方類庫ksoap2,它是一個SOAP Web service客戶端開發(fā)包,主要用于資源受限制的Java環(huán)境如Applets或J2ME應(yīng)用程序(CLDC/ CDC/MIDP)。認真讀完對ksoap2的介紹你會發(fā)現(xiàn)并沒有提及它應(yīng)用于Android平臺開發(fā),沒錯,在Android平臺中我們并不會直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平臺上一個高效、輕量級的SOAP開發(fā)包,等同于Android平臺上的KSoap2的移植版本。
Ksoap2-android jar包下載
//WebService的命名空間
static final String namespace = "";
//服務(wù)器發(fā)布的url
static final String url = ;
final String methodName = "upload"; // 函數(shù)名
final int sessionID = "111111"; //sessionID
//創(chuàng)建HttpTransportSE對象,通過HttpTransportSE類的構(gòu)造方法可以指定WebService的url
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
//指定WebService的命名空間和函數(shù)名
SoapObject soapObject = new SoapObject(namespace, methodName);
//設(shè)置調(diào)用方法參數(shù)的值
soapObject.addProperty("sessionID", sessionID); //sessionID
soapObject.addProperty("data", cds); //cds是需要傳遞的對象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);
//使用call方法調(diào)用WebService方法
transport.call(null, envelope);
SoapObject sb = (SoapObject) envelope.bodyIn;
String xmlMessage = sb.toString(); // 獲取從服務(wù)器端返回的XML字符串
顯示網(wǎng)頁:
1. Uri uri = Uri.parse("");
2. Intent it = new Intent(Intent.ACTION_VIEWuri);
3. startActivity(it);123123
顯示地圖:
1. Uri uri = Uri.parse("geo:38.-77.");
2. Intent it = new Intent(Intent.Action_VIEWuri);
3. startActivity(it);123123
路徑規(guī)劃:
1. Uri uri = Uri.parse("");
2. Intent it = new Intent(Intent.ACTION_VIEWURI);
3. startActivity(it);123123
撥打電話:
調(diào)用撥號程序
1. Uri uri = Uri.parse("tel:xxxxxx");
2. Intent it = new Intent(Intent.ACTION_DIAL uri);
3. startActivity(it);
4. 1. Uri uri = Uri.parse("tel.xxxxxx");
2. Intent it =new Intent(Intent.ACTION_CALLuri);
3. 要運用這個必須在配置文件中加入12345671234567
發(fā)送SMS/MMS
調(diào)用發(fā)送短信的程序
1. Intent it = new Intent(Intent.ACTION_VIEW);
2. it.putExtra("sms_body" "The SMS text");
3. it.setType("vnd.android-dir/mms-sms");
4. startActivity(it);1234512345
發(fā)送短信
1. Uri uri = Uri.parse("smsto:03");
2. Intent it = new Intent(Intent.ACTION_SENDTO uri);
3. it.putExtra("sms_body" "The SMS text");
4. startActivity(it);12341234
發(fā)送彩信
1. Uri uri = Uri.parse("content://media/external/images/media/23");
2. Intent it = new Intent(Intent.ACTION_SEND);
3. it.putExtra("sms_body" "some text");
4. it.putExtra(Intent.EXTRA_STREAM uri);
5. it.setType("image/png");
6. startActivity(it);123456123456
發(fā)送Email
1.
2. Uri uri = Uri.parse("mailto:");
3. Intent it = new Intent(Intent.ACTION_SENDTO uri);
4. startActivity(it);
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_EMAIL "");
3. it.putExtra(Intent.EXTRA_TEXT "The email body text");
4. it.setType("text/plain");
5. startActivity(Intent.createChooser(it "Choose Email Client"));
1. Intent it=new Intent(Intent.ACTION_SEND);
2. String[] tos={""};
3. String[] ccs={""};
4. it.putExtra(Intent.EXTRA_EMAIL tos);
5. it.putExtra(Intent.EXTRA_CC ccs);
6. it.putExtra(Intent.EXTRA_TEXT "The email body text");
7. it.putExtra(Intent.EXTRA_SUBJECT "The email subject text");
8. it.setType("message/rfc822");
9. startActivity(Intent.createChooser(it "Choose Email Client"));12345678910111213141516171819201234567891011121314151617181920
添加附件
1. Intent it = new Intent(Intent.ACTION_SEND);
2. it.putExtra(Intent.EXTRA_SUBJECT "The email subject text");
3. it.putExtra(Intent.EXTRA_STREAM "");
4. sendIntent.setType("audio/mp3");
5. startActivity(Intent.createChooser(it "Choose Email Client"));123456123456
播放多媒體
1.
2. Intent it = new Intent(Intent.ACTION_VIEW);
3. Uri uri = Uri.parse("");
4. it.setDataAndType(uri "audio/mp3");
5. startActivity(it);
1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI "1");
2. Intent it = new Intent(Intent.ACTION_VIEW uri);
3. startActivity(it);123456789123456789
Uninstall 程序
1. Uri uri = Uri.fromParts("package" strPackageName null);
2. Intent it = new Intent(Intent.ACTION_DELETE uri);
3. startActivity(it);12341234
* 安裝指定apk
*
進入聯(lián)系人頁面
Intent intent=newIntent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);12341234
/檢查指定聯(lián)系人
Uri personUri=ContentUris.withAppendedId(People.CONTENT_URI info.id);//info.id聯(lián)系人ID
Intent intent=newIntent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);1234512345
調(diào)用系統(tǒng)安裝一個apk
Intent intent=newIntent();
intent.setDataAndType(Uri.parse()"application/vnd.android.package-archive");
startActivity(intent);123123
//調(diào)用相冊
public static final String MIME_TYPE_IMAGE_JPEG = "image/*";
public static final int ACTIVITY_GET_IMAGE = 0;
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType(MIME_TYPE_IMAGE_JPEG);
startActivityForResult(getImage ACTIVITY_GET_IMAGE);12345671234567
//調(diào)用系統(tǒng)相機應(yīng)用程序,并存儲拍下來的照片
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
time = Calendar.getInstance().getTimeInMillis();
intent.putExtra(MediaStore.EXTRA_OUTPUT Uri.fromFile(new File(Environment
.getExternalStorageDirectory().getAbsolutePath()+"/tucue" time + ".jpg")));
startActivityForResult(intent ACTIVITY_GET_CAMERA_IMAGE);
@paramapkname apk名稱
publicvoidsetupAPK(String apkname){
String fileName=Environment.getExternalStorageDirectory()+"/"+apkname;
Intent intent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(newFile(fileName))"application/vnd.android.package-archive");
mService.startActivity(intent);
}1234567891011121312345678910111213
最近項目中接觸到接口回調(diào),以及Android彈窗PopWindow組件的使用,現(xiàn)在利用學(xué)到的知識自己寫了一個簡單的Demo,練習(xí)下在Android下如何運用接口回調(diào),來實現(xiàn)彈窗PopWindow的功能。
1. 定義一個接口:OnSelectItemListener。定義一個方法 void selectItem(String name, int type),作為點擊彈窗的每個Item的回調(diào)接口。
2. 自定義彈窗類:MyPopupWindow,其布局文件為popup_window.xml。當(dāng)在MainActivity調(diào)用其構(gòu)造函數(shù)創(chuàng)建對象時,同時執(zhí)行initPopupWindow()函數(shù),給每個Item設(shè)置監(jiān)聽器,監(jiān)聽點擊Item時,回調(diào)接口函數(shù)selectItem("Pop Window A", POP_WINDOW_ITEM_1),該函數(shù)在MainActivity中實現(xiàn)。
3. 主Activity: MainActivity。其布局文件為一個Button和一個TextView。監(jiān)聽Button,每當(dāng)點擊則彈出PopWindow,呈現(xiàn)三個Item。調(diào)用MyPopupWindow類中的方法setOnSelectItemListener(OnSelectItemListener listener),傳入OnSelectItemListener 對象作為參數(shù),同時實現(xiàn)回調(diào)接口OnSelectItemListener的方法void selectItem(String name, int type)。
主Activity: MainActivity. Java
[java] view plain copy
packagecom.lambdroid.callbacktest2;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.widget.Toast;
//聯(lián)系接口的回調(diào)以及PopWindow彈窗的簡單使用
publicclassMainActivityextendsActivity?{
privateMyPopupWindow?myPopupWindow;
privateButton?btn_pop_window;
privateTextView?tv_display;
protectedContext?context;
@Override
protectedvoidonCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context?=this;
btn_pop_window?=?(Button)?findViewById(R.id.btn_pop_window);
tv_display?=?(TextView)?findViewById(R.id.tv_display);
//給Button設(shè)置事件監(jiān)聽:彈出彈窗
btn_pop_window.setOnClickListener(newView.OnClickListener()?{
@Override
publicvoidonClick(View?v)?{
myPopupWindow.show(btn_pop_window);
}
});
myPopupWindow?=newMyPopupWindow(context);
//實現(xiàn)OnSelectItemListener接口的selectItem方法:對于彈窗三個Item的事件監(jiān)聽
myPopupWindow.setOnSelectItemListener(newOnSelectItemListener()?{
@Override
publicvoidselectItem(String?name,inttype)?{
//點擊電站列表,彈出彈框
if(myPopupWindow?!=null?myPopupWindow.isShowing())?{
myPopupWindow.dismiss();
}
tv_display.setText(name);
switch(type){
caseMyPopupWindow.POP_WINDOW_ITEM_1:
Toast.makeText(context,"我是彈窗A,?我的英文名是"+?name,?Toast.LENGTH_SHORT).show();
break;
caseMyPopupWindow.POP_WINDOW_ITEM_2:
Toast.makeText(context,"我是彈窗B,?我的英文名是"+?name,?Toast.LENGTH_SHORT).show();
break;
caseMyPopupWindow.POP_WINDOW_ITEM_3:
Toast.makeText(context,"我是彈窗C,?我的英文名是"+?name,?Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
}
}
activity_main.xml
[html] view plain copy
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/btn_pop_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:padding="20dp"
android:text="Pop?Window"
android:textSize="20sp"/
android:id="@+id/tv_display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Hello?World!"
android:textSize="30sp"/
自定義彈窗類:MyPopupWindow.java
[java] view plain copy
packagecom.lambdroid.callbacktest2;
importandroid.app.ActionBar;
importandroid.content.Context;
importandroid.graphics.drawable.ColorDrawable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.widget.LinearLayout;
importandroid.widget.PopupWindow;
publicclassMyPopupWindowimplementsView.OnClickListener{
privatePopupWindow?mPopWindow;
privateContext?mContext;
privateLinearLayout?llPop1;
privateLinearLayout?llPop2;
privateLinearLayout?llPop3;
privateintpw_height;
publicstaticfinalintPOP_WINDOW_ITEM_1?=1;
publicstaticfinalintPOP_WINDOW_ITEM_2?=2;
publicstaticfinalintPOP_WINDOW_ITEM_3?=3;
privateOnSelectItemListener?listener;
publicvoidsetOnSelectItemListener(OnSelectItemListener?listener){
this.listener?=?listener;
}
publicMyPopupWindow(Context?context){
mContext?=?context;
initPopupWindow();//初始化彈窗
}
publicvoidinitPopupWindow(){
View?view?=?LayoutInflater.from(mContext).inflate(R.layout.popup_window,null);
mPopWindow?=newPopupWindow(view,?ActionBar.LayoutParams.WRAP_CONTENT,?ActionBar.LayoutParams.WRAP_CONTENT,true);
mPopWindow.setOutsideTouchable(true);
/**?為其設(shè)置背景,使得其內(nèi)外焦點都可以獲得?*/
mPopWindow.setBackgroundDrawable(newColorDrawable());
mPopWindow.setFocusable(true);
pw_height?=?view.getHeight();
llPop1?=?(LinearLayout)?view.findViewById(R.id.ll_pop_1);
llPop1.setOnClickListener(this);
llPop2?=?(LinearLayout)?view.findViewById(R.id.ll_pop_2);
llPop2.setOnClickListener(this);
llPop3?=?(LinearLayout)?view.findViewById(R.id.ll_pop_3);
llPop3.setOnClickListener(this);
}
//監(jiān)聽三個彈窗的點擊事件
@Override
publicvoidonClick(View?v)?{
switch(v.getId()){
caseR.id.ll_pop_1:
if(listener?!=null)?{
listener.selectItem("Pop?Window?A",?POP_WINDOW_ITEM_1);//回調(diào)接口
}
break;
caseR.id.ll_pop_2:
if(listener?!=null)?{
listener.selectItem("Pop?Window?B",?POP_WINDOW_ITEM_2);
}
break;
caseR.id.ll_pop_3:
if(listener?!=null)?{
listener.selectItem("Pop?Window?C",?POP_WINDOW_ITEM_1);
}
break;
default:
break;
}
}
//顯示彈窗,并設(shè)置彈窗基于標(biāo)題欄的顯示位置
publicvoidshow(View?view)?{
//popupwindow相對view位置x軸偏移量
View?viewTemp?=?mPopWindow.getContentView();
viewTemp.measure(0,0);
intwidth?=?viewTemp.getMeasuredWidth();
intxOffset?=?(view.getWidth()?-?width)?/2;
mPopWindow.showAsDropDown(view,?xOffset,0);
}
/**
*?退出popupwindow
*/
publicvoiddismiss()?{
if(mPopWindow?!=null?mPopWindow.isShowing())?{
mPopWindow.dismiss();
}
}
/**
*?popupwindow是否正在顯示
*/
publicbooleanisShowing()?{
if(mPopWindow?!=null)?{
returnmPopWindow.isShowing();
}
returnfalse;
}
}
popup_window.xml
[html] view plain copy
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ll_alarm_type"
android:layout_width="120dp"
android:layout_height="130dp"
android:orientation="vertical"
android:background="@drawable/popupwindow"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:id="@+id/ll_pop_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="窗口?A"
android:textSize="15sp"
android:textColor="#ffffff"/
android:id="@+id/ll_pop_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="窗口?B"
android:textSize="15sp"
android:textColor="#ffffff"/
android:id="@+id/ll_pop_3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="窗口?C"
android:textSize="15sp"
android:textColor="#FFFFFF"/
回調(diào)接口:OnSelectItemListener
[java] view plain copy
packagecom.lambdroid.callbacktest2;
publicinterfaceOnSelectItemListener?{
voidselectItem(String?name,inttype);
}
點擊Button,彈出彈窗,顯示三個Item
點擊第二個Item,通過回調(diào)函數(shù),來實現(xiàn)TextView內(nèi)容的修改,以及彈出Toast
總結(jié)
Java回調(diào)情形涉及很多,本文屬于接口的異步回調(diào):當(dāng)不知道何時會執(zhí)行接口的回調(diào)函數(shù),(通過接口回調(diào)來對獲取到的資源的操作)。除此還有線程間的異步回調(diào)(子線程進行耗時操作,操作完畢通知主線程或?qū)?shù)據(jù)傳給主線程處理),以及利用接口回調(diào)來實現(xiàn)線程間的數(shù)據(jù)通信等等(Android可以利用Handler來實現(xiàn))。等下次再舉例說明Java回調(diào)函數(shù)的其它情形。
首先要知道第三方App的activity的包名和啟動Activity是哪一個,然后可以通過包名直接調(diào)用起來。當(dāng)然Intent的時候要注意設(shè)置Flag是NewTask