zoukankan      html  css  js  c++  java
  • Service的学习代码

    1. startService(new Intent(MainActivity.this, MyService.class))------->stopService(new Intent(MainActivity.this, MyService.class))

    2.bindService(new Intent(MainActivity.this, MyService.class), mServiceConnection,Context.BIND_AUTO_CREATE)---->

    if(mServiceConnection != null){unbindService(mServiceConnection);mServiceConnection = null;
    }----->一般放在onDestory()函数里面

    MainActivity.java

    private MyService myService;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
    Log.i("jxy",this.getClass().getName() + "--->onServiceConnected");
    myService = ((MyService.MyBinder)iBinder).getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
    Log.i("jxy",this.getClass().getName() + "--->onServiceDisconnected");
    }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    if(mServiceConnection != null){
    unbindService(mServiceConnection);
    mServiceConnection = null;
    }
    }

    public void startService(View view) {
    startService(new Intent(MainActivity.this, MyService.class));
    }

    public void stopService(View view) {
    stopService(new Intent(MainActivity.this, MyService.class));
    }

    public void bindService(View view) {
    bindService(new Intent(MainActivity.this, MyService.class), mServiceConnection,
    Context.BIND_AUTO_CREATE);
    }

    public void mp3player(View view){
    myService.mp3Play((String) view.getTag());
    }

    MyService.java
    class MyBinder extends Binder{
    public MyService getService(){
    return MyService.this;
    }
    }
    @Override
    public IBinder onBind(Intent intent) {
    // Log.i("jxy",this.getClass().getName() + "--->onBind");
    // Binder binder = new Binder();
    // Log.i("jxy",this.getClass().getName() + "--->binder:" + binder);
    return new MyBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
    Log.i("jxy",this.getClass().getName() + "--->onUnbind");
    return super.onUnbind(intent);
    }

    @Override
    public void onCreate() {
    super.onCreate();
    Log.i("jxy",Thread.currentThread().getName() + ":MyService --> onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("jxy",Thread.currentThread().getName() + "MyService --> onStartCommand");
    return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
    super.onDestroy();
    Log.i("jxy",Thread.currentThread().getName() + "MyService --> onDestroy");
    }

    // 此方法完成MP3播放的功能
    public void mp3Play(String name){
    Log.i("jxy",this.getClass().getName() + "--->正在播放的歌曲名称为:" + name);
    }

    activity.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="16dp">

    <Button
    android:onClick="startService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="启动服务" />

    <Button
    android:onClick="stopService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="停止服务" />
    <Button
    android:onClick="bindService"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="绑定服务" />
    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="解绑服务" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="调用系统服务挂断电话" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="测试黑名单拦截(调用系统服务)" />

    <Button
    android:tag="相信自己"
    android:onClick="mp3player"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="音乐播放测试" />

    </LinearLayout>

    Manifest.xml文件的service的注册
    <service android:name=".MyService" />




  • 相关阅读:
    获取当前div中的所有div的个数和每一个div的ID and 根据屏幕分辨率计算高度
    在当前页获取父窗口中母版页中的服务器控件的ID
    Asp.net C# 获取本周上周本月上月本年上年第一天最后一天时间大全
    C#后台调用前台js方法
    IComparable与排序
    C# 与.NET2.0 中类型Type的GetMethod方法
    下拉菜单及时间段的获取
    黑马程序员——JAVA基础之简述集合collection
    黑马程序员——JAVA基础之基本数据类型包装类和1.5JDK新特性装箱
    黑马程序员——JAVA基础之String和StringBuffer
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9901326.html
Copyright © 2011-2022 走看看