android Service显示Notification浅析

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必须调用,不然不能更新通知。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值