android开发

本文介绍了一个关于Android中Activity与Service之间通信的示例程序。该程序通过广播接收器实现Activity与Service之间的数据交换,展示了如何从Service向Activity发送数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原地址http://www.dnbcw.com/biancheng/java/lfwk176836.html

//设置高宽
// LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
// LinearLayout.LayoutParams.FILL_PARENT,
// LinearLayout.LayoutParams.WRAP_CONTENT
// );

简介:这是Activity和Service之间通讯的Demo的详细页面,介绍了和java,JavaEye Activity和Service之间通讯的Demo有关的知识,加入收藏请按键盘ctrl+D,谢谢大家的观看!要查看更多有关信息,请点击此处
1.Activity的类别文件:

package wyf.wpf;//声明包语句

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
//继承自Activity的子类

public class Sample_3_6 extends Activity {
public static final int CMD_STOP_SERVICE = 0;
Button btnStart;// 开始服务Button对象应用
Button btnStop;// 停止服务Button对象应用
TextView tv;// TextView对象应用
DataReceiver dataReceiver;// BroadcastReceiver对象

@Override
public void onCreate(Bundle savedInstanceState) {// 重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);// 设置显示的屏幕
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
tv = (TextView) findViewById(R.id.tv);

btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Sample_3_6.this,
wyf.wpf.MyService.class);
startService(myIntent);// 发送Intent启动Service
}
});

btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent();
myIntent.setAction("wyf.wpf.MyService");
myIntent.putExtra("cmd", CMD_STOP_SERVICE);
sendBroadcast(myIntent);// 发送广播
}
});
}

private class DataReceiver extends BroadcastReceiver {// 继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {// 重写onReceive方法
double data = intent.getDoubleExtra("data", 0);
tv.setText("Service的数据为:" + data);
}
}

@Override
protected void onStart() {// 重写onStart方法
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
filter.addAction("wyf.wpf.Sample_3_6");
registerReceiver(dataReceiver, filter);// 注册Broadcast Receiver
super.onStart();
}

@Override
protected void onStop() {// 重写onStop方法
unregisterReceiver(dataReceiver);// 取消注册Broadcast Receiver
super.onStop();
}
}


2.服务的类:

package wyf.wpf;//声明包语句

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
//继承自Service的子类

public class MyService extends Service {
CommandReceiver cmdReceiver;
boolean flag;

@Override
public void onCreate() {// 重写onCreate方法
flag = true;
cmdReceiver = new CommandReceiver();
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {// 重写onBind方法
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {// 重写onStartCommand方法
IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
filter.addAction("wyf.wpf.MyService");
registerReceiver(cmdReceiver, filter);// 注册Broadcast Receiver,后续会接收相关广播intent
doJob();// 调用方法启动线程
return super.onStartCommand(intent, flags, startId);
}

// 方法:
public void doJob() {
new Thread() {
public void run() {
while (flag) {
try {// 睡眠一段时间
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent();// 创建Intent对象
intent.setAction("wyf.wpf.Sample_3_6");
intent.putExtra("data", Math.random());
sendBroadcast(intent);// 发送广播
}
}

}.start();
}

private class CommandReceiver extends BroadcastReceiver {// 继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {// 重写onReceive方法
int cmd = intent.getIntExtra("cmd", -1);// 获取Extra信息
if (cmd == Sample_3_6.CMD_STOP_SERVICE) {// 如果发来的消息是停止服务
flag = false;// 停止线程
stopSelf();// 停止服务
}
}
}

@Override
public void onDestroy() {// 重写onDestroy方法
this.unregisterReceiver(cmdReceiver);// 取消注册的CommandReceiver
super.onDestroy();
}
}


最后是配置文件和main.xml,每个Service都需要在配置文件里申明标签:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wyf.wpf" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Sample_3_6" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" android:process=":remote">
<intent-filter>
<action android:name="wyf.wpf.MyService" />
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="7" />

</manifest>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnStart" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="启动服务" />
<Button android:id="@+id/btnStop" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="停止服务" />
<TextView android:id="@+id/tv" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="等待来自Service的数据" />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值