• andorid service 本地服务


    ActivityManifect.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hanqi.testservice">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <service
                android:name=".TestService1"
                android:enabled="true"
                android:exported="true"></service>
        </application>
    
    </manifest>

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.hanqi.testservice.MainActivity"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="一般启动服务"
                android:onClick="bt1_onclick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="一般停止服务"
                android:onClick="bt2_onclick"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="绑定启动服务"
                android:onClick="bt3_onclick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="解绑停止服务"
                android:onClick="bt4_onclick"/>
        </LinearLayout>
    
    
    </LinearLayout>

    TestService.java

    package com.hanqi.testservice;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.util.Log;
    
    public class TestService1 extends Service {
        public TestService1() {
    
            Log.e("TAG","构造Service");
        }
        //绑定的回调方法
        //必须要重写
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            //throw new UnsupportedOperationException("Not yet implemented");
            Log.e("TAG","绑定被调用");
            return new Binder();
        }
        @Override
        public void onCreate() {
            Log.e("TAG","创建Service");
            super.onCreate();
        }
    
        @Override
        public void onDestroy() {
            Log.e("TAG","销毁Service");
            super.onDestroy();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e("TAG","启动命令");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.e("TAG","解除绑定");
            return super.onUnbind(intent);
        }
    }

    mainactivity.java

    package com.hanqi.testservice;
    
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        //一般方式启动Service
        //参考Activity的启动方式
        public void bt1_onclick(View v)
        {
            //通过意图Intent启动
            //显示意图
            Intent intent = new Intent(this,TestService1.class);
            startService(intent);
            Toast.makeText(MainActivity.this, "启动Service", Toast.LENGTH_SHORT).show();
    
        }
        //一般方式停止Service
        //参考Activity的启动方式
        public void bt2_onclick(View v)
        {
            //通过意图Intent启动
            //显示意图
            Intent intent = new Intent(this,TestService1.class);
            stopService(intent);
            Toast.makeText(MainActivity.this, "停止Service", Toast.LENGTH_SHORT).show();
    
        }
    
        ServiceConnection serviceConnection;
        public void bt3_onclick(View v)
        {
    
            //通过意图Intent启动
            //显示意图
            Intent intent = new Intent(this,TestService1.class);
    
            if(serviceConnection == null) {
                serviceConnection = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        Log.e("TAG", "连接服务");
    
                    }
    
                    @Override
                    public void onServiceDisconnected(ComponentName name) {
    
                        Log.e("TAG", "断开链接服务");
                    }
                };
            }
            //参数: 意图,连接,绑定方式   BIND_AUTO_CREATE(自动创建)
            bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    
            Toast.makeText(MainActivity.this, "绑定服务成功", Toast.LENGTH_SHORT).show();
        }
    
        public void bt4_onclick(View v)
        {
            //判断是否已经绑定,连接是否为空
            if(serviceConnection != null)
            {
                //解除绑定
                unbindService(serviceConnection);
    
                serviceConnection = null;
    
                Toast.makeText(MainActivity.this, "解除绑定", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(MainActivity.this, "尚未绑定", Toast.LENGTH_SHORT).show();
            }
        }
    
        //  Acticity销毁的回调方法
        @Override
        protected void onDestroy() {
    
            if(serviceConnection != null) {
                //解除绑定
                unbindService(serviceConnection);
    
                serviceConnection = null;
            }
            super.onDestroy();
        }
    }
  • 相关阅读:
    MySQL数据库06 /数据库总结
    数据库05 /索引、数据库备份、锁和事务、慢查询优化、索引命中相关
    数据库04 /多表查询、pymysql模块
    数据库03 /库、表、记录的详细操作、单表查询
    struts基于ognl的自动类型转换需要注意的地方
    struts-json-plugin result中配置对象的序列化
    hibernate关联关系笔记
    使用动态代理实现数据库事务(转)
    xml、文件操作功能类
    将CachedRowSet中的数据转储到对象中
  • 原文地址:https://www.cnblogs.com/cuikang/p/5423694.html
Copyright © 2020-2023  润新知