程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JAVA完成KMP算法實際和示例代碼

JAVA完成KMP算法實際和示例代碼

編輯:關於JAVA

JAVA完成KMP算法實際和示例代碼。本站提示廣大學習愛好者:(JAVA完成KMP算法實際和示例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA完成KMP算法實際和示例代碼正文


Service的性命周期 (實用於2.1及以上)

1. 被startService的
不管能否有任何運動綁定到該Service,都在後台運轉。onCreate(若須要) -> onStart(int id, Bundle args).  屢次startService,則onStart挪用屢次,但不會創立多個Service實例,只須要一次stop。該Service一向後台運轉,直到stopService或許本身的stopSelf()或許資本缺乏由平台停止。

2. 被bindService的
挪用bindService綁定,銜接樹立辦事一向運轉。未被startService只是BindService,則onCreate()履行,onStart(int,Bundle)不被挪用;這類情形下綁定被消除,平台便可以消除該Service(銜接燒毀後,會招致消除,消除後就會燒毀)。

3. 被啟動又被綁定
相似startService的性命周期,onCreate onStart都邑挪用。

4. 停滯辦事時
stopService時顯式onDestroy()。或不再有綁定(沒有啟動時)時隱式挪用。有bind情形下stopService()不起感化。

以下是一個簡略的完成例子,某些部門須要合營logcat不雅察。
AcMain.java


package jtapp.myservicesamples;
import android.app.Activity;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AcMain extends Activity implements OnClickListener {
        private static final String TAG = "AcMain";
        private Button btnStart;
        private Button btnStop;
        private Button btnBind;
        private Button btnExit;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findView();
    }
        private void findView() {
                btnStart = (Button) findViewById(R.id.Start);
        btnStop = (Button) findViewById(R.id.Stop);
        btnBind = (Button) findViewById(R.id.Bind);
        btnExit = (Button) findViewById(R.id.Exit);

        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnBind.setOnClickListener(this);
        btnExit.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
                Intent intent = new Intent("jtapp.myservicesamples.myservice");
                switch(v.getId()) {
                case R.id.Start:
                        startService(intent);
                        Toast.makeText(this,
                                        "myservice running " + MyService.msec/1000.0 + "s.",
                                        Toast.LENGTH_LONG).show();
                        break;
                case R.id.Stop:
                        stopService(intent);
                        Toast.makeText(this,
                                        "myservice running " + MyService.msec/1000.0 + "s.",
                                        Toast.LENGTH_LONG).show();
                        break;
                case R.id.Bind:
                        bindService(intent, sc, Context.BIND_AUTO_CREATE);
                        break;
                case R.id.Exit:
                        this.finish();
                        break;
                }
        }

        private MyService serviceBinder;

        private ServiceConnection sc = new ServiceConnection() {
                @Override
                public void onServiceDisconnected(ComponentName name) {
                        Log.d(TAG, "in onServiceDisconnected");
                        serviceBinder = null;
                }
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                        Log.d(TAG, "in onServiceConnected");
                        serviceBinder = ((MyService.MyBinder)service).getService();
                }
        };
        @Override
        protected void onDestroy() {
                //this.unbindService(sc);
                //this.stopService(
                //                new Intent("jtapp.myservicesamples.myservice"));
                super.onDestroy();
        }
}

main.xml


<?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:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="@string/hello" />
        <Button android:text="Start MyService" android:id="@+id/Start"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>
        <Button android:text="Stop MyService" android:id="@+id/Stop"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>
        <Button android:text="Bind MyService" android:id="@+id/Bind"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>
        <Button android:text="Exit AcMain" android:id="@+id/Exit"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

MyService.java


package jtapp.myservicesamples;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
        private static final String TAG = "MyService";
        public static long msec = 0;
        private boolean bThreadRunning = true;

       
        private final IBinder binder = new MyBinder();
        public class MyBinder extends Binder {
                MyService getService() {
                        return MyService.this;
                }
        }
        @Override
        public IBinder onBind(Intent intent) {
                return binder;
        }
        @Override
        public void onCreate() {
                new Thread(new Runnable(){
                        @Override
                        public void run() {
                                while (bThreadRunning) {
                                        try {
                                                Thread.sleep(100);
                                        } catch (InterruptedException e) {
                                        }
                                        Log.i(TAG, "myservice running " + (msec+=100) + "ms.");
                                }
                        }
                }).start();
        }
        @Override
        public void onDestroy() {
                bThreadRunning = false;
                super.onDestroy(); // 可以不消
        }
}

AnndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="jtapp.myservicesamples" android:versionCode="1"
        android:versionName="1.0">
        <application android:icon="@drawable/icon" android:label="@string/app_name"
                android:debuggable="true">
                <activity android:name=".AcMain" 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">
                        <intent-filter>
                                <action android:name="jtapp.myservicesamples.myservice"></action>
                        </intent-filter>
                </service>
        </application>
</manifest>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved