Serivce通知Activity更新UI方法总结

Activity与Service是Android的两个重要组件,在使用过程中我们遇到最多的是他们之间通讯的问题。

1、Activity向Service传递消息的方法:
    1)利用BroadcastReceiver发送广播,Activity发送广播,Service中定义广播接收者进行接收。
    2)利用绑定服务的方式开启服务,暴露服务中的方法,Activity进行调用。
    3)利用Intent打开服务(开启服务)的方式,通过Intent传递数据。

2、Service向Activity传递消息的方法:
    1)利用BroadcastReceiver,在Service中发送广播,Activity中接收。
    2)利用Handler在Service中发送消息,Activity中handleMessage进行处理。

下面以音乐播放器案例来,对以上的方式进行示例说明。
在这个案例中,Activity界面按钮(播放、暂停、停止)要发送消息给Service(负责音乐播放),Service要把播放进度通知给Activity进行播放进度的更新。

程序的界面很简单:
Serivce通知Activity更新UI方法总结 - ppy2790@126 - ITAIR
 
 
一、BroadcastReceiver实现Activity与Service相互通讯:
Activity中:

//点击播放、暂停按钮

public void play(View view) {

Intent intent = new Intent();

intent.putExtra("ctrl", 1);

intent.setAction("org.itair.service.receiver");

sendBroadcast(intent);

}


//点击停止按钮

public void stop(View view) {

Intent intent = new Intent();

intent.putExtra("ctrl", 3);

intent.setAction("org.itair.service.receiver");

sendBroadcast(intent);

}



------------------------------------------------------------------------


Service中动态注册一个广播接收者:

ServiceRecevier serviceRecevier = new ServiceRecevier();

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction("org.itair.service.receiver");

registerReceiver(serviceRecevier, intentFilter);


Service中的BroadcastReceiver,收到后,同时也向Activity发送广播,(同样在Activity中也需要注册广播接收者)

public class ServiceRecevier extends BroadcastReceiver {


@Override

public void onReceive(Context context, Intent intent) {

// 接收到Activity播放消息类型后,进行判断,调用服务中播放,暂停,停止的方法

int ctrl = intent.getExtras().getInt("ctrl", -1);

int type = -1;

switch (ctrl) {

case 1:

type = 1;

if (!isPlay) {

prepareAndStart(0, current);

} else {

pause();

}

break;

case 2:

pause();

break;

case 3:

stop();

type = 0;

break;

}


//接收消息后,同时向Activit发送在播放状态,播放曲目id给Activity

//Activity收到消息后,会播放按钮更新为暂停状态

Intent sendIntent = new Intent();

sendIntent.putExtra("type", type);

sendIntent.putExtra("status", isPlay);

sendIntent.setAction("org.itair.ui.receiver");

sendBroadcast(sendIntent);


}


}


Service中音乐播放时把播放进度用广播发送给Activity:

       //Service中播放音乐的方法

public void prepareAndStart(int index, int current) {


try {

AssetFileDescriptor afd = am.openFd(musics[index]);

mPlayer.reset();

mPlayer.setDataSource(afd.getFileDescriptor(),

afd.getStartOffset(), afd.getLength());


mPlayer.prepare();

mPlayer.seekTo(current);

mPlayer.start();


isPlay = true;


final int total = mPlayer.getDuration();

Runnable runnable = new Runnable() {

Intent sendIntent = new Intent();


@Override

public void run() {

// TODO Auto-generated method stub


while (isPlay) {

// 发送进度广播给Activity

sendIntent.putExtra("type", 2);

sendIntent.putExtra("current",

100 * mPlayer.getCurrentPosition() / total);

sendIntent.setAction("org.itair.ui.receiver");

sendBroadcast(sendIntent);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}

};


new Thread(runnable).start();


mPlayer.setOnCompletionListener(new OnCompletionListener() {


@Override

public void onCompletion(MediaPlayer mp) {

// TODO Auto-generated method stub

//播放完毕发送消息给Activity

isPlay = false;

Intent sendIntent = new Intent();

sendIntent.putExtra("type", 0);

sendIntent.putExtra("status", isPlay);

sendIntent.setAction("org.itair.ui.receiver");

sendBroadcast(sendIntent);


}

});


} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


----------------------------------------------------------------------


 相应的,Activity中的广播接收者,接收广播进行处理:

public class UiRecevier extends BroadcastReceiver {


@Override

public void onReceive(Context context, Intent intent) {

int type = intent.getIntExtra("type", -1);

switch (type) {

//更新播放按钮

case 0:

pb.setProgress(0);

btn_start.setImageResource(R.drawable.play);

break;

case 1:

boolean isPlay = intent.getExtras().getBoolean("status");

tv_singer.setText(titles[0]);


if (isPlay) {

btn_start.setImageResource(R.drawable.pause);

} else {

btn_start.setImageResource(R.drawable.play);

}

break;


case 2:

//更新进度

pb.setProgress(intent.getExtras().getInt("current"));

break;

}

}

}


二、采用绑定服务开启服务的方法暴露Service中的方法,Activity进行调用,Service利用Handler向Activity发送进度消息。(绑定服务+Handler进行通讯)


Activity中绑定服务:

conn = new MyConn();

Intent intent = new Intent(this, MusicService.class);

bindService(intent, conn, BIND_AUTO_CREATE);


Service中向Activity中发送播放进度消息(Handler+Message)


Runnable runnable = new Runnable() {

Intent sendIntent = new Intent();


Message message = null;

@Override

public void run() {

// TODO Auto-generated method stub

while (isPlay) {

//Handler message实现向Activity发送播放进度消息

message = new Message();

message.arg1 = 100 * mPlayer.getCurrentPosition()

/ total;

MainActivity.handler.sendMessage(message);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}

};


new Thread(runnable).start();


----------------------------------------------------------------------

注意这个handler是在Activity中声明定义的,且要声明为public static。

Activity中,handler接收到消息后进行进度条的更新。


                public static Handler handler;


handler = new Handler() {


@Override

public void handleMessage(Message msg) {

// TODO Auto-generated method stub

pb.setProgress(msg.arg1);

if(msg.arg2==100){

pb.setProgress(0);

btn_start.setImageResource(R.drawable.play);

isPlay = false;

}

                                super.handleMessage(msg);

}

};


三、其它方式:
      Activity向Service发送消息,还可以采用Intent,Service采用开启服务( startService )的方式。
      Service向Activity发送消息,网上也有说可以采用Intent打开Activity的方法,但用在播放器更新进度时,不太合适。

Service可以通过使用Android系统提供的AlarmManager来实现定时发送通知的功能。具体步骤如下: 1. 在Service中创建一个PendingIntent对象,用于在触发时启动一个广播。 2. 使用AlarmManager的set方法来设置定时器,指定触发时间和要启动的广播。 3. 在广播接收器中实现发送通知的逻辑。 4. 在AndroidManifest.xml文件中注册Service和广播接收器。 下面是一个简单的示例代码,演示如何实现每天定时发送通知: ``` public class MyService extends Service { private static final int NOTIFICATION_ID = 1; private static final long INTERVAL_DAY = 24 * 60 * 60 * 1000; @Override public int onStartCommand(Intent intent, int flags, int startId) { // 创建PendingIntent对象,用于在触发时启动一个广播 Intent alarmIntent = new Intent(this, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); // 获取AlarmManager对象,并设置定时器 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerTime(), INTERVAL_DAY, pendingIntent); return super.onStartCommand(intent, flags, startId); } // 计算定时器触发时间 private long getTriggerTime() { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (calendar.getTimeInMillis() < System.currentTimeMillis()) { calendar.add(Calendar.DAY_OF_MONTH, 1); } return calendar.getTimeInMillis(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 发送通知 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_launcher_background) .setContentTitle("定时通知") .setContentText("今天是新的一天,加油哦!") .setAutoCancel(true); Notification notification = builder.build(); notificationManager.notify(NOTIFICATION_ID, notification); } } <service android:name=".MyService" /> <receiver android:name=".MyReceiver" /> ``` 这段代码会在每天早上8点定时发送一条通知。在实际开发中,可以根据需要修改触发时间、通知内容等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值