Android中Service发送广播给activity

Android中,service和activity是2大组件,如何在后台运行service,并且控制前台activity的显示呢,这里用到了广播。

这里就不废话了,先贴activity代码:

<span style="font-size:12px;">package com.example.broadcasewithservice;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv=(TextView)findViewById(R.id.tv);
		
		Intent intent = new Intent(MainActivity.this,MyService.class);
		this.startService(intent);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		registerReceiver();  
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(mReceiver);
	}
  
	//注册广播
	public void registerReceiver() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("<span style="color:#ff0000;">com.zhanghao.msg</span>");
		registerReceiver(mReceiver, intentFilter);
	}
	
	
	//接收广播
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equals("com.zhanghao.msg")){
				String stringExtra = intent.getStringExtra("msg");
				tv.setText(stringExtra);
			}
		}
	};
	
}
</span>


然后贴上service的代码:

<span style="font-size:12px;">package com.example.broadcasewithservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		Log.i("MyService", "onBind");
		sendMsg("onBind");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.i("MyService", "onCreate");
		sendMsg("onCreate");
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("MyService", "onDestroy");
		sendMsg("onDestroy");
	}

	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		super.onRebind(intent);
		Log.i("MyService", "onRebind");
		sendMsg("onRebind");
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		Log.i("MyService", "onStart");
		sendMsg("onStart");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Log.i("MyService", "onUnbind");
		sendMsg("onUnbind");
		return super.onUnbind(intent);
	}

	</span><span style="font-size:12px;"><span style="color:#ff0000;">// 发送广播
	private void sendMsg(String msg) {
		Intent intent = new Intent("com.zhanghao.msg");
		intent.putExtra("msg", msg);
		this.sendBroadcast(intent);
	}

</span>}
</span>


这里的广播注册是在代码中注册的。也可以在AndroidManifast.xml注册,这里就不废话了。service需要在AndroidManifast.xml注册,代码:

<span style="font-size:12px;"><application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        </span><span style="font-size:12px;"><span style="color:#ff0000;"><service android:name=".MyService"></service> 
</span>        
        <activity
            android:name="com.example.broadcasewithservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application></span>


这是一个简单的demo,代码书写力求简单,所以有很多方面没有考虑到。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值