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" />




  • 相关阅读:
    leetcode701. Insert into a Binary Search Tree
    leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
    leetcode 110. Balanced Binary Tree
    leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
    二叉树
    leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
    5. Longest Palindromic Substring
    128. Longest Consecutive Sequence
    Mac OS下Android Studio的Java not found问题,androidfound
    安卓 AsyncHttpClient
  • 原文地址:https://www.cnblogs.com/liunx1109/p/9901326.html
Copyright © 2011-2022 走看看