使用support v4包里的LocalBroadcastManager类方便得进行Service和Activity的交互
2012-06-05 18:07
274 查看
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uk.ac.essex.LocalServiceBroadcaster" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LocalServiceBroadcasterActivity" 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="LocalServiceBroadcasterActivity$LocalService" android:stopWithTask="true" > </service> </application> </manifest>
LocalBroadcastManagerActivity类:
package uk.ac.essex.LocalServiceBroadcaster; import android.app.Activity; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.support.v4.app.ServiceCompat; import android.support.v4.content.LocalBroadcastManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class LocalServiceBroadcasterActivity extends Activity { static final String ACTION_STARTED = "com.example.android.supportv4.STARTED"; static final String ACTION_UPDATE = "com.example.android.supportv4.UPDATE"; static final String ACTION_STOPPED = "com.example.android.supportv4.STOPPED"; LocalBroadcastManager mLocalBroadcastManager; BroadcastReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // This is where we print the data we get back. final TextView callbackData = (TextView) findViewById(R.id.callback); // Put in some initial text. callbackData.setText("No broadcast received yet"); // We use this to send broadcasts within our local process. mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); // We are going to watch for interesting local broadcasts. IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_STARTED); filter.addAction(ACTION_UPDATE); filter.addAction(ACTION_STOPPED); mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_STARTED)) { callbackData.setText("STARTED"); } else if (intent.getAction().equals(ACTION_UPDATE)) { callbackData.setText("Got update: " + intent.getIntExtra("value", 0)); } else if (intent.getAction().equals(ACTION_STOPPED)) { callbackData.setText("STOPPED"); } } }; mLocalBroadcastManager.registerReceiver(mReceiver, filter); // Watch for button clicks. Button button = (Button) findViewById(R.id.start); button.setOnClickListener(mStartListener); button = (Button) findViewById(R.id.stop); button.setOnClickListener(mStopListener); } @Override protected void onDestroy() { super.onDestroy(); mLocalBroadcastManager.unregisterReceiver(mReceiver); } private OnClickListener mStartListener = new OnClickListener() { public void onClick(View v) { startService(new Intent(LocalServiceBroadcasterActivity.this, LocalService.class)); } }; private OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { stopService(new Intent(LocalServiceBroadcasterActivity.this, LocalService.class)); } }; public static class LocalService extends Service { LocalBroadcastManager mLocalBroadcastManager; int mCurUpdate; static final int MSG_UPDATE = 1; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_UPDATE: { mCurUpdate++; Intent intent = new Intent(ACTION_UPDATE); intent.putExtra("value", mCurUpdate); mLocalBroadcastManager.sendBroadcast(intent); Message nmsg = mHandler.obtainMessage(MSG_UPDATE); mHandler.sendMessageDelayed(nmsg, 1000); } break; default: super.handleMessage(msg); } } }; @Override public void onCreate() { super.onCreate(); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); } public int onStartCommand(Intent intent, int flags, int startId) { // Tell any local interested parties about the start. mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STARTED)); // Prepare to do update reports. mHandler.removeMessages(MSG_UPDATE); Message msg = mHandler.obtainMessage(MSG_UPDATE); mHandler.sendMessageDelayed(msg, 1000); return ServiceCompat.START_STICKY; } @Override public void onDestroy() { super.onDestroy(); // Tell any local interested parties about the stop. mLocalBroadcastManager.sendBroadcast(new Intent(ACTION_STOPPED)); // Stop doing updates. mHandler.removeMessages(MSG_UPDATE); } @Override public IBinder onBind(Intent intent) { return null; } } }
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="4dip" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" android:text="演示如何使用LocalBroadcastManager来进行Service和Activity之间的通信" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启服务" > <requestFocus /> </Button> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止服务" > </Button> <TextView android:id="@+id/callback" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" android:gravity="center_horizontal" android:paddingTop="4dip" /> </LinearLayout>
记得要加上android-support-v4.jar并加入classpath
相关文章推荐
- Delphi Android ActivityManager(提供了接口, 利用它可以方便的对Memory, Processes, Task, Service 等进行管)
- 使用FLEX的HttpService与ASP.NET进行动态交互之一
- 使用FLEX的HttpService与ASP.NET进行动态交互之一
- 在Activity和Service之间使用Binder和回调接口进行通信
- Android解决activity、service间使用sharePreference进行值存取不同步
- Android Activity 与Service进行数据交互详解
- Activity和Service 使用Binder进行通信
- 编写一个Activity基类,对Activity进行封装,方便使用
- 利用Intent使Activity和Service通过BroadcastReceiver进行数据交互
- 使用Messenger进行Activity与Service之间的双向通信
- android开发之使用Messenger实现service与activity交互
- 使用flex中的httpservice方法与java进行交互
- Activity与Service进行数据交互
- 使用BroadCast 使 service 和activity 信息交互
- android开发之使用Messenger实现service与activity交互
- 使用BroadCast 使 service 和activity 信息交互
- 使用Handler实现Activity和Service之间的交互
- android开发之使用Messenger实现service与activity交互
- 使用FLEX的HttpService与ASP.NET进行动态交互
- android开发之使用Messenger实现service与activity交互