安卓一个小程序回调函数打开另外一个小程序回调函数怎么回调后怎么知道它结束了

Android中实现一个应用程序对另一个应用程序的调用_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Android中实现一个应用程序对另一个应用程序的调用
来源:Linux社区&
作者:Eagle
在app1中写下下面的一端代码:
&&&&&& Intent tIntent = new Intent();&&&&&& ComponentName tComp = new ComponentName("app2.eagle", "app2.eagle.EagleZip");&&&&&& tIntent.setComponent(tComp);&&&&&& tIntent.setAction(".intent.action.MAIN");&&&&&& tIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
&&&&&& startActivity(tIntent);
&&& 在app1中的AndroidMenifest.xml中有这样的配置:
&&&&&&&&&&& &activity android:name="UI"&&&&&&&&&&&&&&&& android:label="@string/app_name"&&&&&&&&&&&&&&&&& &intent-filter&&&&&&&&&&&&&&&&&&&&& &action android:name="android.intent.action.MAIN" /&&&&&&&&&&&&&&&&&&&&& &category android:name="android.intent.category.LAUNCHER" /&&&&&&&&&&&&&&&&& &/intent-filter&&&&&&&&&&&&&&&&& &intent-filter&&&&&&&&&&&&&&&&&&&&& &action android:name="UI" /&&&&&&&&&&&&&&&&&&&&&& &category android:name="android.intent.category.DEFAULT"/& &&&&&&&&&&&&&&&& &/intent-filter&&&&&&&&&&&& &&&&&&&&&&& &/activity&
&&& 在app2的AndroidMenifest.xml中下面的配置:
&&&&&&&&&& &activity android:name="EagleZip"&&&&&&&&&&&&&&&&& android:label="@string/app_name"&&&&&&&&&&&&&&&&&& &intent-filter&&&&&&&&&&&&&&&&&&&&&& &action android:name="android.intent.action.MAIN" /&&&&&&&&&&&&&&&&&&&&&& &category android:name="android.intent.category.LAUNCHER" /&&&&&&&&&&&&&&&&&& &/intent-filter&&&&&&&&&&&&&&&&&& &intent-filter&&&&&&&&&&&&&&&&&&&&&& &action android:name="EagleZip" /&&&&&&&&&&&&&&&&&&&&&& &category android:name="android.intent.category.DEFAULT"/& &&&&&&&&&&&&&&&&& &/intent-filter&
&&&&&&&&&&& &/activity&
相关资讯 & & &
& (07/13/:14)
& (08/11/:27)
& (05/06/:13)
& (04/17/:32)
& (05/31/:15)
& (04/02/:07)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款Android程序员必须知道的53个知识点_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Android程序员必须知道的53个知识点
阅读已结束,下载文档到电脑
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩43页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢急:在另一个应用程序能调用新浪微博的android客户端打开一条微博吗,或打开一个微博主页吗
全部答案(共4个回答)
你好,1、某些品牌型号的行货手机内置有微博客户端,发微博的时候可以显示手机型号,比如三星Galaxy Note,Galaxy S 2,HTC灵感等等。如果你的手...
因为您更新的是新的安卓客户端起而不是手机型号客户端
1、想要使用手机发微博显示对应的手机型号,建议卸载当前来源不正确的微博版本,再通过手机自带的“应用商店”下载最新微博客户端尝试看看。
若仍不显示,请提供对应截...
根据您的描述,由于软件版本的不同,显示的信息会有所不同。此属正常现象,请您放心使用。如需显示手机型号,请您更新微博版本或下载其他版本尝试。
是手机网络问题(如若,您对我的答复满意,请选择“对我有用”,谢谢您的采纳。)
确定举报此问题
举报原因(必选):
广告或垃圾信息
激进时政或意识形态话题
不雅词句或人身攻击
侵犯他人隐私
其它违法和不良信息
报告,这不是个问题
报告原因(必选):
这不是个问题
这个问题分类似乎错了
这个不是我熟悉的地区
相关问答:123456789101112131415android callRemoteService 调用其他进程的service
创建一个进程服务.程序第一次运行的时候就创建.不需要界面
要想其他进程能够跨进程的调用本进程的方法.就得使用跨进程访问的接口类型.只需将接口中类和方法的修饰符去掉就行.然后把接口类aidl类型
服务也业务代码:
package com.example.&
import android.app.S&
import android.content.I&
import android.os.IB&
import android.os.RemoteE&
&* 跨进程访问的接口类型:将接口和接口中方法的修饰类型去掉,
&* android生成aidl文件步骤:找到编写的java源文件,将其扩展名该为aidl.然后回到项目
&* ,刷新项目,该类就会变为源文件名.aidl的文件,并且会自动在gen
&* .工程目录下生成原java文件的名的java文件.可以打开看下内容与之前不一样了.到此就完成了aidl的创建
&* @author Administrator
public class RemoteService extends Service {&
&&& @Override&
&&& public IBinder onBind(Intent arg0) {&
&&&&&&& return new Mybinder();&
&&&& * 可以继承系统根据自己定义的iservice接口生成iservice.adil之后的ISerice.Stub类,
&&&& * Stub是系统自己家上的.该类是个抽象类,继承了binder类也实现了iservice&br&
&&&& * @author Administrator
&&& public class Mybinder extends IServcice.Stub {&
&&&&&&& @Override&
&&&&&&& public void callMethodInService() throws RemoteException {&
&&&&&&&&&&& sayHelloInService();&
&&&&&&& }&
&&&& * 服务中的方法
&&& public void sayHelloInService() {&
&&&&&&& System.out.println(&hello in service&);&
&&& @Override&
&&& public void onCreate() {&
&&&&&&& System.out.println(&remote service create&);&
服务进程对外提供调用的接口,将该接口转成aidl文件,
aidl文件的创建:找到编写的java源文件,将其扩展名该为aidl.然后回到项目,刷新项目,该类就会变为源文件名.aidl的文件,并且系统会自动在gen .工程目录下生成原java文件的名的java文件.可以打开看下内容与之前不一样了.到此就完成了aidl的创建
package com.example.&
interface IServcice {&
&&& void callMethodInService();&
在配置文件application中注册该服务
&service android:name=&.RemoteService& &&
&&&&&&&&&& &intent-filter&&
&&&&&&&&&&&&&& &action android:name=&com.example.remoteservice& /& &!-- 指定action,可方便别的程序调用 --&&
&&&&&&&&&& &/intent-filter&&
&&&&&& &/service&&
到此被调用的服务就创建好了.如果想对外使用.必须启动该进程.和服务.所以.为配合下面调用的例子(调用外部进程的服务)必须保证该进程的正常运行.
总结以上调用本进程中服务的方法:
&1创建一个服务,该服务中要有能被调用的方法;
&2:定义一个借口IService, 接口里面的抽象方法去调用服务中的方法 ;
&3:定义一个MyBinder对象.继承binder类,并实现IService接口,在onBind方法中返回MyBinder对象;
&4:在activity中通过bindService ()绑定一个服务;
&5:创建一个MyConn实现ServiceConnection接口 ,服务绑定成功后执行接口中的onServiceConnected()方法;
&6:onServiceConnected ()方法的IBinder参数就是IService类型的,强制转换为IS
&7:调用ISservice中方法
调用外部进程中服务(上面的进程)
访问其他进程中服务的步骤:
&访问外部服务.要知道对方服务的action,和可以调用服务接口的adil文件以及adil文件的包名.在本地程序创建调用服务的aidl文件的包,并添加(copy)外部服务的aidl文件,系统会自动在gen目录下创建该adil文件接口java文件
&1创建一个服务,该服务中要有能被调用的方法;
&2:定义一个借口IService, 接口里面的抽象方法去调用服务中的方法,去掉定义接口和接口中方法的修饰符,把这个接口的扩展名该为.aidl,;
&3:定义一个MyBinder对象,继承编译器根据IService.aidl在gen中创建的IService类中的Sutb类,
&在onBind方法中返回MyBinder对象
&4:在调用服务的工程中创建包,包名要与被调用服务工程中对应aidl文件所在的包名一样.并复制被调用工程对应的aidl文件到本地对应aidl文件包中
&5:在activity中通过bindService ()绑定一个服务;
&6:创建一个MyConn实现ServiceConnection接口 ,服务绑定成功后执行接口中的方法;
&7:在实现接口中的onServiceConnected()方法调用IServcice.Stub.asInterface()方法.将onServiceConnected的IBinder参数转换成IService类型;
&8:调用IService中的方法(调用服务中的方法),
按照上面步骤创建了同样的包和copy了被调用的aidl文件之后,创建调用外部进程页面的布局.本例只以一个button为例
&&& android:id=&@+id/call&&
&&& android:layout_width=&wrap_content&&
&&& android:layout_height=&wrap_content&&
&&& android:onClick=&click&&
&&& android:text=&调用外部服务& /&&
调用外部服务的代码实现
package com.example.&
import com.example.remoteservice.IS&
import android.app.A&
import ponentN&
import android.content.C&
import android.content.I&
import android.content.ServiceC&
import android.os.B&
import android.os.IB&
import android.os.RemoteE&
import android.view.V&
&* 访问外部服务.要知道对方服务的action,和可以调用服务接口的adil文件以及adil文件的包名&br&
&* 在本地程序创建调用服务的adil文件的包,并添加外部服务的adil文件,系统会自动在gen目录下创建该adil文件接口java文件&br&
&* 总结:&br&
&* 访问本进程中的service,需要用到bindService()方法.(cz23_serverlifecycle)&br&
&* 1创建一个服务,该服务中要有能被调用的方法;&br&
&* 2:定义一个借口IService, 接口里面的抽象方法去调用服务中的方法 ;&br&
&* 3:定义一个MyBinder对象.继承binder类,并实现IService接口,在onBind方法中返回MyBinder对象;&br&
&* 4:在activity中通过bindService ()绑定一个服务;&br&
&* 5:创建一个MyConn实现ServiceConnection接口 ,服务绑定成功后执行接口中的onServiceConnected()方法;&br&
&* 6:onServiceConnected ()方法的IBinder参数就是IService类型的,强制转换为IS&br&
&* 7:调用ISservice中方法&br&
&* 访问 外部进程中的service,需要用到aidl(android interface definition language)(在编号24的两个例子)&br&
&* 1创建一个服务,该服务中要有能被调用的方法;&br&
&* 2:定义一个借口IService, 接口里面的抽象方法去调用服务中的方法,去掉定义接口和接口中方法的修饰符,把这个接口的扩展名该为.aidl,;&br&
&* 3:定义一个MyBinder对象,继承编译器根据IService.aidl在gen中创建的IService类中的Sutb类,
&* 在onBind方法中返回MyBinder对象&br&
&* 4:在调用服务的工程中创建包,包名要与被调用服务工程中对应aidl文件所在的包名一样.并复制被调用工程对应的aidl文件到本地对应aidl文件包中&br&
&* 5:在activity中通过bindService ()绑定一个服务;&br&
&* 6:创建一个MyConn实现ServiceConnection接口 ,服务绑定成功后执行接口中的方法;&br&
&* 7:在实现接口中的onServiceConnected()方法调用IServcice.Stub.asInterface()方法.
&* 将onServiceConnected的IBinder参数转换成IService类型&br&
&* 8:调用IService中的方法(调用服务中的方法),
&* @author Administrator
public class MainActivity extends Activity {&
&&& private IServcice iS&
&&& @Override&
&&& public void onCreate(Bundle savedInstanceState) {&
&&&&&&& super.onCreate(savedInstanceState);&
&&&&&&& setContentView(R.layout.activity_main);&
&&&&&&& Intent intent = new Intent();&
&&&&&&& intent.setAction(&com.example.remoteservice&);// 隐式意图,指定访问服务的action,就是被调用服务进程值注册服务的时候指定的action的值&
&&&&&&& bindService(intent, new MyConn(), Context.BIND_AUTO_CREATE);// 通过隐式意图绑定到外部服务.参数2是服务绑定成功后的一个回调类.通过他可以获取外部服务对外提供数据的对象&
&&& public void click(View view) {&
&&&&&&& try {&
&&&&&&&&&&& iServcice.callMethodInService();&
&&&&&&& } catch (RemoteException e) {&
&&&&&&&&&&& // TODO Auto-generated catch block&
&&&&&&&&&&& e.printStackTrace();&
&&&&&&& }&
&&& class MyConn implements ServiceConnection {&
&&&&&&& @Override&
&&&&&&& /**
&&&&&&&& * 参数IBinder就是绑定被调用的服务执行onBind的返回值
&&&&&&&& */&
&&&&&&& public void onServiceConnected(ComponentName arg0, IBinder service) {&
&&&&&&&&&&& iServcice = IServcice.Stub.asInterface(service);// 调用外部进程中service的转换方法&
&&&&&&& }&
&&&&&&& @Override&
&&&&&&& public void onServiceDisconnected(ComponentName arg0) {&
&&&&&&&&&&& // TODO Auto-generated method stub&
&&&&&&& }&Android 在一个程序中启动另一个程序(包名,或者类名) - 海王 - 博客园
随笔 - 548
Android 开发有时需要在一个应用中启动另一个应用,比如Launcher加载所有的已安装的程序的列表,当点击图标时可以启动另一个应用。一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动:Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);&&& &&& &&&&ComponentName cn = new ComponentName(packageName, className);&&& &&& &&&&intent.setComponent(cn);startActivity(intent);但是更多的时候,我们一般都不知道应用程序的启动Activity的类名,而只知道包名,我们可以通过ResolveInfo类来取得启动Acitivty的类名。下面是实现代码:private void openApp(String packageName) {PackageInfo pi = getPackageManager().getPackageInfo(packageName, 0);Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);resolveIntent.setPackage(pi.packageName);List&ResolveInfo& apps = pm.queryIntentActivities(resolveIntent, 0);ResolveInfo ri = apps.iterator().next();if (ri != null ) {String packageName = ri.activityInfo.packageNString className = ri.activityInfo.Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);ComponentName cn = new ComponentName(packageName, className);intent.setComponent(cn);startActivity(intent);}}
阅读(...) 评论()
Copyright & 海王
Powered by:
模板提供:}

我要回帖

更多关于 微信小程序 回调 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信