• Android-Service开发步骤


    Main

    package com.lxt008;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class Main extends Activity implements OnClickListener  {
        private MyService  mMyService;   
        private TextView mTextView;   
        private Button startServiceButton;   
        private Button stopServiceButton;  
       
        private Context mContext;       
       
        public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.main);   
            setupViews();   
        }          
        public void setupViews(){ 
            
            mContext = Main.this; 
            
            mTextView = (TextView)findViewById(R.id.text);        
               
            startServiceButton = (Button)findViewById(R.id.startservice);   
            stopServiceButton = (Button)findViewById(R.id.stopservice);   
             
            startServiceButton.setOnClickListener(this);   
            stopServiceButton.setOnClickListener(this);   
            
        }     
        public void onClick(View v) {         
            if(v == startServiceButton){   
                Intent i  = new Intent();   
                i.setClass(Main.this, MyService.class);   
                mContext.startService(i);   
            }else if(v == stopServiceButton){   
                Intent i  = new Intent();   
                i.setClass(Main.this, MyService.class);   
                mContext.stopService(i);   
            } 
        }        
    }

    MyService

    package com.lxt008;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.text.format.Time;
    import android.util.Log;
    
    public class MyService extends Service {
        
        // 定义个一个Tag标签
        private static final String TAG = "MyService";   
        // 一个Binder类,用在onBind()有方法里,这样Activity那边可以获取到
        private MyBinder mBinder = new MyBinder(); 
        
        public IBinder onBind(Intent intent) {   
            Log.e(TAG, "start IBinder~~~");   
            return mBinder;   
        }      
        public void onCreate() {   
            Log.e(TAG, "start onCreate~~~");   
            super.onCreate();   
        }    
        public void onStart(Intent intent, int startId) {   
            Log.e(TAG, "start onStart~~~");   
            super.onStart(intent, startId);    
        }        
        public void onDestroy() {   
            Log.e(TAG, "start onDestroy~~~");   
            super.onDestroy();   
        }        
        public boolean onUnbind(Intent intent) {   
            Log.e(TAG, "start onUnbind~~~");   
            return super.onUnbind(intent);   
        }      
        public String getSystemTime(){           
            Time t = new Time();   
            t.setToNow();   
            return t.toString();   
        }        
        public class MyBinder extends Binder{   
            MyService getService()   
            {   
                return MyService.this;   
            }   
        } 
     }

    布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <TextView android:id="@+id/text" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="@string/hello" />
        <Button android:id="@+id/startservice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="startService" />
        <Button android:id="@+id/stopservice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:text="stopService" />
    </LinearLayout>

    添加注册

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.lxt008"
          android:versionCode="1"
          android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".Main"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        <!-- 添加注册 -->
            <service android:name=".MyService" android:exported="true"></service>   
    
        </application>
        <uses-sdk android:minSdkVersion="7" />
    
    </manifest> 
  • 相关阅读:
    map映射的用法
    相似的字串(hash+二分)
    进制 /字符串 hash
    CF#632 C.Eugene and an array
    2020牛客寒假算法基础集训营6 H-云
    Educational Codeforces Round 80 (Div. 2)
    Codeforces Round #601 (Div. 2)补题
    luogu-单调队列/单调栈专题
    Comet OJ
    Comet OJ
  • 原文地址:https://www.cnblogs.com/spadd/p/4190021.html
Copyright © 2020-2023  润新知