Service使用Notification变成前台进程。
Notification可以自定义界面:
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="font"
android:layout_alignParentLeft="true"
android:layout_below="@id/progressbar1"
></Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/progressbar1"
android:text="next" />
</RelativeLayout>
2、服务定义
public class MusicService extends Service {
RemoteViews remoteView;
ProgressBar progressbar;
Notification notification;
NotificationManager mNotificationManager;
@Override
public void onCreate() {
Log.v("service", "onCreate");
initNotification();
mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
super.onCreate();
}
@Override
public void onDestroy() {
Log.v("service", "Destroy");
super.onDestroy();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
Log.v("service", "Start");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("service", "startCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
Log.v("service", "bind");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.v("service", "onUnbind");
return super.onUnbind(intent);
}
public void update(int num) {
notification.contentView.setProgressBar(R.id.progressbar1, 100, num,
false);
startForeground(R.drawable.ic_launcher, notification);
}
MyBinder binder = new MyBinder();
public class MyBinder extends Binder {
public int num = 0;
public int get() {
return num;
}
public void set(int n) {
num = n;
update(num);
}
}
@SuppressLint("NewApi")
private void initNotification() {
int version = android.os.Build.VERSION.SDK_INT;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
0, intent, 0);
remoteView = new RemoteViews(getPackageName(),
R.layout.notification_main);
remoteView.setProgressBar(R.id.progressbar1, 100, 10, false);
remoteView.setOnClickFillInIntent(R.id.button1, new Intent(
Intent.ACTION_DIAL));
remoteView.setOnClickPendingIntent(R.id.button2, PendingIntent
.getService(getApplicationContext(), 0, new Intent(
getApplicationContext(), MusicService.class), 0));
if (version >= 16) {
Notification.Builder n = new Notification.Builder(this)
.setAutoCancel(true).setContentTitle("title")
.setContentText("text").setContentIntent(pi)
.setSmallIcon(R.drawable.flymessage)
.setWhen(System.currentTimeMillis());
notification = n.build();
notification.contentView = remoteView;
} else if (version > 11 && version < 16) {
Notification.Builder builder = new Notification.Builder(this)
.setAutoCancel(true).setContentTitle("Title")
.setContentText("Text").setContentIntent(pi)
.setSmallIcon(R.drawable.flymessage)
.setWhen(System.currentTimeMillis());
notification = builder.getNotification();
} else {
notification = new Notification();
notification.icon = R.drawable.flymessage;
notification.tickerText = "通知栏信息";
notification.when = System.currentTimeMillis();
notification.setLatestEventInfo(this, "contentTitle",
"contentText", pi);
}
startForeground(R.drawable.ic_launcher, notification);
}
}
在服务的update函数中更新进度条。注意startForeground必须调用,不然不能更新通知。