Android Api Demos登顶之路(五十七)Service Remote Service Binding options
2015-09-05 10:58
525 查看
这个demo演示了绑定服务时,各种标志符的使用。
服务端还是上节中的RemoteService
客户端
布局文件:
activity_main.xml
MainActivity.java
服务端还是上节中的RemoteService
客户端
布局文件:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/state" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Not Attached" android:textAppearance="?android:attr/textAppearanceMedium" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Normal" /> <Button android:id="@+id/not_foreground" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Not Foreground" /> <Button android:id="@+id/above_client" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Above Client" /> <Button android:id="@+id/oom_management" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Allow Oom Management" /> <Button android:id="@+id/waive_priority" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Waive Priority" /> <Button android:id="@+id/important" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Important" /> <Button android:id="@+id/with_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Adjust With Activity" /> <Button android:id="@+id/unbinding" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Unbind Service" /> </LinearLayout> </ScrollView> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnClickListener { private TextView mState; private ServiceConnection mCurConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mState = (TextView) findViewById(R.id.state); Button normal = (Button) findViewById(R.id.normal); Button not_foreground = (Button) findViewById(R.id.not_foreground); Button above_client = (Button) findViewById(R.id.above_client); Button oom_management = (Button) findViewById(R.id.oom_management); Button waive_priority = (Button) findViewById(R.id.waive_priority); Button important = (Button) findViewById(R.id.important); Button with_activity = (Button) findViewById(R.id.with_activity); Button unbinding = (Button) findViewById(R.id.unbinding); normal.setOnClickListener(this); not_foreground.setOnClickListener(this); above_client.setOnClickListener(this); oom_management.setOnClickListener(this); waive_priority.setOnClickListener(this); important.setOnClickListener(this); with_activity.setOnClickListener(this); unbinding.setOnClickListener(this); } class MyServiceConnection implements ServiceConnection { public MyServiceConnection() { } @Override public void onServiceConnected(ComponentName name, IBinder service) { // 如果当前的连接不是由本类创建的一个实例则不作处理 if (mCurConnection != this) { return; } mState.setText("Attached!"); Toast.makeText(MainActivity.this, "已与服务器建立连接!", 0).show(); } @Override public void onServiceDisconnected(ComponentName name) { if (mCurConnection != this) { return; } mState.setText("Disconnected!"); Toast.makeText(MainActivity.this, "已与服务器断开连接!", 0).show(); } } private Intent intent = new Intent(IRemoteService.class.getName()); ServiceConnection conn = null; private void unbindCurService() { if (mCurConnection != null) { unbindService(mCurConnection); mCurConnection = null; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.normal: unbindCurService(); conn = new MyServiceConnection(); /* * 当接收到连接的请求时,如果服务还未创建,则自动创建服务。当系统资源不足时, * 当需要 销毁驻留该服务的进程时,服务才可能被销毁。 */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) { mCurConnection = conn; } break; case R.id.not_foreground: unbindCurService(); conn = new MyServiceConnection(); /* * 所绑定的Service永远高不过前台的Activity */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND)) { mCurConnection = conn; } break; case R.id.above_client: unbindCurService(); conn = new MyServiceConnection(); /* * 所绑定的Service优先级高于前台的Activity,当系统资源不足时会先杀死 * 客户端的activity再考虑杀死Service */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_ABOVE_CLIENT)) { mCurConnection = conn; } break; case R.id.oom_management: unbindCurService(); conn = new MyServiceConnection(); /* * 允许系统托管服务的优先级 */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_ALLOW_OOM_MANAGEMENT)) { mCurConnection = conn; } break; case R.id.waive_priority: unbindCurService(); conn = new MyServiceConnection(); /* * 所绑定的Service不可调整自身的优先级 */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY)) { mCurConnection = conn; } break; case R.id.important: unbindCurService(); conn = new MyServiceConnection(); /* * 当客户端在前台,这个标示符下的Service也变得重要性相当于前台的Activity, * 优先级迅速提升 */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT)) { mCurConnection = conn; } break; case R.id.with_activity: unbindCurService(); conn = new MyServiceConnection(); /* * Service的优先级相对于与其绑定的Activity进行调整,当Activity运行在前台 * 则Service的优先级提升,当Activity运行在后台,则Service的优先级也相对降低 */ if (bindService(intent, conn, Context.BIND_AUTO_CREATE | Context.BIND_ADJUST_WITH_ACTIVITY)) { mCurConnection = conn; } break; case R.id.unbinding: unbindCurService(); break; } } }
相关文章推荐
- Android 实现适配器中的子控件与Activity通信
- Android ListView —— Adapter, BaseAdapter, RecycleBin
- android fragment 详解(二)
- Android应用程序的权限列表
- Android View.OnTouchListener 的子类,AutoScrollHelper,ZoomButtonsController,ListViewAutoScrollHelper
- 关于android studio的代码显示行数的问题
- android ListView 防止图片上拉跳动
- [转]Android Parcelable和Serializable的区别
- Android笔记:Fragment与ViewPager组合时,如何在FragmentActicity获取Fragment对象
- Android Studio error and config collection
- Android -- Contentprovider---listview 实现调用通讯录和短信接收
- android开发中如何获得应用的数字签名
- ANDROID开发之SQLite详解
- Android Studio如何快速生成get,set,tostring,构造函数
- Android基础View 绘制的流程
- Android中activity间数据传递方式
- Android 同一Activity 静态加载fragment 并实现通信
- android菜鸟进阶之路——线程的基本探究
- phonegap android插件,启动activity并返回值
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)