前台service和远程service代码详解
来源:互联网 发布:政府网络舆情管理 编辑:程序博客网 时间:2024/06/03 12:37
因为之前对service不是很熟,所以进一步研究了一下
//有时候需要前台service,如墨迹天气(在状态栏中).下面是简单的一个前台Service
public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();System.out.println("------------->执行");new Thread(new Runnable() {@Overridepublic void run() {Notification notification = new Notification(R.drawable.ic_launcher, "有通知到来", System.currentTimeMillis()); Intent notificationIntent = new Intent(MyService.this, Main.class); PendingIntent pendingIntent = PendingIntent.getActivity(MyService.this, 0, notificationIntent, 0); //设置信息内容 notification.setLatestEventInfo(MyService.this, "这是通知的标题", "这是通知的内容", pendingIntent); startForeground(1, notification); //让Service变成一个前台Service }}).start();}}
/*************************************************************AIDL Service**********************************************************************************************************************/
服务端:新建一个工程:AIDL_Service
新建一个aidl文件
package com.example.aidl;interface AIDLservice{ int plus(int a, int b);}
//MyService.java
public class AIDLservice extends Service {@Overridepublic IBinder onBind(Intent intent) {return stub;}//在AIDLservice.java文件可知道Stub是Binder的子类com.example.aidl.AIDLservice.Stub stub=new Stub() {@Overridepublic int plus(int a, int b) throws RemoteException {return a+b;}};}//androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aidl_service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.aidl_service.Main" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--添加此属性:android:process=":remote" 表示为远程service--> <service android:name="com.example.aidl_service.AIDLservice" android:process=":remote"> <intent-filter > <!--此处体现了隐示Intent--> <action android:name="com.example.aidl_service.startAIDLservice"/> </intent-filter> </service> </application></manifest>
//客户端:新建工程AIDL_Client
/** * 要实现远程Service的功能,必须将远程Service端的aidl文件原始的复制到客户端的src下*/public class Main extends Activity { private Button button;private AIDLservice aidLservice;//远程Service接口@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//匿名类,主要是通过该匿名类与service通信final ServiceConnection conn=new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {try {aidLservice=AIDLservice.Stub.asInterface(service); int num=aidLservice.plus(5, 4);Log.d("------------>num=", num+"");} catch (Exception e) {System.out.println("连接失败!!!");e.printStackTrace();}}};button=(Button)this.findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setAction("com.example.aidl_service.startAIDLservice");bindService(intent, conn, BIND_AUTO_CREATE);}});}}猛击下载
0 0
- 前台service和远程service代码详解
- Android_Service(2)前台服务(service)和远程服务(service)
- 前台Service
- 前台Service
- 【Service】创建前台Service
- Service和远程控制
- 5、Service详解(三):远程Service
- Android Service详解(八)---前台服务详解
- 【Service】Service长时间前台运行
- AIDL和远程Service调用
- AIDL和远程Service调用
- AIDL和远程Service调用
- AIDL和远程Service调用
- AIDL和远程Service调用
- AIDL和远程Service调用
- AIDL和远程Service调用
- AIDL和远程Service调用
- Android 远程 service 和 Binder
- 自己看的书--2014年5月27日
- C++中不应该返回局部变量的地址
- 【LeetCode】Remove Duplicates from Sorted List I && II
- bzoj 1084: [SCOI2005]最大子矩阵 题解
- ASP 组件简介之ASP Content Linking组件
- 前台service和远程service代码详解
- CF cut ribbon
- jxl 打印excel,样式设置
- android 如何实现移动数据上网的开关?
- hdu 1875 畅通工程再续
- 树状数组
- [ALGO-20] 求先序排列
- 数据结构----顺序表的实现
- IOS_UI_Picker