因为之前对service不是很熟,所以进一步研究了一下
//有时候需要前台service,如墨迹天气(在状态栏中).下面是简单的一个前台Service
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("------------->执行");
new Thread(new Runnable() {
@Override
public 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 {
@Override
public IBinder onBind(Intent intent) {
return stub;
}
//在AIDLservice.java文件可知道Stub是Binder的子类
com.example.aidl.AIDLservice.Stub stub=new Stub() {
@Override
public 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接口
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//匿名类,主要是通过该匿名类与service通信
final ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public 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() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("com.example.aidl_service.startAIDLservice");
bindService(intent, conn, BIND_AUTO_CREATE);
}
});
}
}
猛击下载