重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
1、对应一个接口即可,比如OnMsgChangeListener,代码:
创新互联-专业网站定制、快速模板网站建设、高性价比华池网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式华池网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖华池地区。费用合理售后完善,10多年实体公司更值得信赖。
public interface OnMsgChangeListener {
public void doMsg(MsgBean bean);
}
2、定义MsgBeanActivity处理MsgBean数据
3、定义MsgBean实体
4、在MsgBeanActivity定义setMsgChangeListener()方法,传入的OnMsgChangeListener接口对象,如下代码:
setMsgChangeListener(OnMsgChangeListener onMsg)
5、重写接口方法
在Android平台调用Web Service需要依赖于第三方类库ksoap2,它是一个SOAP Web service客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP)。认真读完对ksoap2的介绍你会发现并没有提及它应用于Android平台开发,没错,在Android平台中我们并不会直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一个高效、轻量级的SOAP开发包,等同于Android平台上的KSoap2的移植版本。
Ksoap2-android jar包下载
//WebService的命名空间
static final String namespace = "";
//服务器发布的url
static final String url = ;
final String methodName = "upload"; // 函数名
final int sessionID = "111111"; //sessionID
//创建HttpTransportSE对象,通过HttpTransportSE类的构造方法可以指定WebService的url
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
//指定WebService的命名空间和函数名
SoapObject soapObject = new SoapObject(namespace, methodName);
//设置调用方法参数的值
soapObject.addProperty("sessionID", sessionID); //sessionID
soapObject.addProperty("data", cds); //cds是需要传递的对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);
//使用call方法调用WebService方法
transport.call(null, envelope);
SoapObject sb = (SoapObject) envelope.bodyIn;
String xmlMessage = sb.toString(); // 获取从服务器端返回的XML字符串
显示网页:
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
路径规划:
1. Uri uri = Uri.parse("");
2. Intent it = new Intent(Intent.ACTION_VIEWURI);
3. startActivity(it);123123
拨打电话:
调用拨号程序
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
发送SMS/MMS
调用发送短信的程序
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
发送短信
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
发送彩信
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
发送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
*
进入联系人页面
Intent intent=newIntent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);12341234
/检查指定联系人
Uri personUri=ContentUris.withAppendedId(People.CONTENT_URI info.id);//info.id联系人ID
Intent intent=newIntent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);1234512345
调用系统安装一个apk
Intent intent=newIntent();
intent.setDataAndType(Uri.parse()"application/vnd.android.package-archive");
startActivity(intent);123123
//调用相册
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
//调用系统相机应用程序,并存储拍下来的照片
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
最近项目中接触到接口回调,以及Android弹窗PopWindow组件的使用,现在利用学到的知识自己写了一个简单的Demo,练习下在Android下如何运用接口回调,来实现弹窗PopWindow的功能。
1. 定义一个接口:OnSelectItemListener。定义一个方法 void selectItem(String name, int type),作为点击弹窗的每个Item的回调接口。
2. 自定义弹窗类:MyPopupWindow,其布局文件为popup_window.xml。当在MainActivity调用其构造函数创建对象时,同时执行initPopupWindow()函数,给每个Item设置监听器,监听点击Item时,回调接口函数selectItem("Pop Window A", POP_WINDOW_ITEM_1),该函数在MainActivity中实现。
3. 主Activity: MainActivity。其布局文件为一个Button和一个TextView。监听Button,每当点击则弹出PopWindow,呈现三个Item。调用MyPopupWindow类中的方法setOnSelectItemListener(OnSelectItemListener listener),传入OnSelectItemListener 对象作为参数,同时实现回调接口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;
//联系接口的回调以及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设置事件监听:弹出弹窗
btn_pop_window.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View v) {
myPopupWindow.show(btn_pop_window);
}
});
myPopupWindow =newMyPopupWindow(context);
//实现OnSelectItemListener接口的selectItem方法:对于弹窗三个Item的事件监听
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);
/** 为其设置背景,使得其内外焦点都可以获得 */
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);
}
//监听三个弹窗的点击事件
@Override
publicvoidonClick(View v) {
switch(v.getId()){
caseR.id.ll_pop_1:
if(listener !=null) {
listener.selectItem("Pop Window A", POP_WINDOW_ITEM_1);//回调接口
}
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;
}
}
//显示弹窗,并设置弹窗基于标题栏的显示位置
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"/
回调接口:OnSelectItemListener
[java] view plain copy
packagecom.lambdroid.callbacktest2;
publicinterfaceOnSelectItemListener {
voidselectItem(String name,inttype);
}
点击Button,弹出弹窗,显示三个Item
点击第二个Item,通过回调函数,来实现TextView内容的修改,以及弹出Toast
总结
Java回调情形涉及很多,本文属于接口的异步回调:当不知道何时会执行接口的回调函数,(通过接口回调来对获取到的资源的操作)。除此还有线程间的异步回调(子线程进行耗时操作,操作完毕通知主线程或将数据传给主线程处理),以及利用接口回调来实现线程间的数据通信等等(Android可以利用Handler来实现)。等下次再举例说明Java回调函数的其它情形。
首先要知道第三方App的activity的包名和启动Activity是哪一个,然后可以通过包名直接调用起来。当然Intent的时候要注意设置Flag是NewTask