美文网首页
Android服务的一个最小示例

Android服务的一个最小示例

作者: 汶水一方 | 来源:发表于2017-11-23 07:16 被阅读13次
    1. 新建项目,一切默认。
    2. 新建一个Service,此处取名为Accelerometer。写入内容如下:
    package com.rokid.accelerometerservice;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    
    public class Accelerometer extends Service {
        public Accelerometer() {
        }
    
        public final String TAG = "fyg";
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(TAG, "onCreate: Service Created");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d(TAG, "onStartCommand: Service Started");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            Log.d(TAG, "onDestroy: Service Destroyed");
            super.onDestroy();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            Log.d(TAG, "onBind: Service Binded");
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
    
    1. MainActivity.java的内容修改如下:
    package com.rokid.accelerometerservice;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button startService = (Button) findViewById(R.id.btnStartService);
            Button stopService = (Button) findViewById(R.id.btnStopService);
    
            startService.setOnClickListener(this);
            stopService.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btnStartService:
                    Intent startIntent = new Intent(this, Accelerometer.class);
                    startService(startIntent);
                    break;
    
                case R.id.btnStopService:
                    Intent stopIntent = new Intent(this, Accelerometer.class);
                    stopService(stopIntent);
                    break;
    
                default:
                    break;
    
            }
        }
    }
    
    1. res/layout/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:orientation="vertical"
        tools:context="com.rokid.accelerometerservice.MainActivity">
    
        <Button
            android:id="@+id/btnStartService"
            android:text="Start Service"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/btnStopService"
            android:text="Stop Service"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    
    1. 检查一下AndroidManifest.xml的内容:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.rokid.accelerometerservice">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            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=".Accelerometer"
                android:enabled="true"
                android:exported="true">
    
            </service>
        </application>
    
    </manifest>
    
    screenshot
    1. 运行后,点击按钮,在AS上的日志窗口查看日志输出。
    onCreate: Service Created
    onStartCommand: Service Started
    onDestroy: Service Destroyed
    

    测试通过。

    相关文章

      网友评论

          本文标题:Android服务的一个最小示例

          本文链接:https://www.haomeiwen.com/subject/sdpavxtx.html