《android關係Service的基礎用法》

主Activity:主要就是監聽事件啓動Servicejava

package com.example.jserver;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
 private Button onSer;
 private Button stopSer;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  onSer=(Button) findViewById(R.id.ser);
  stopSer=(Button) findViewById(R.id.stopSer);
  
  ButtonClick click=new ButtonClick();
  onSer.setOnClickListener(click);
  stopSer.setOnClickListener(click);
 }
 
 
 class ButtonClick implements OnClickListener{
  
  @Override
  public void onClick(View view) {
   Button but=(Button) view;
   Intent intent=new Intent();
   intent.setClass(MainActivity.this, JService.class);
   if(R.id.ser==but.getId()){
    Toast.makeText(MainActivity.this, "進來了On", Toast.LENGTH_SHORT).show();
    startService(intent);
   }else{
    Toast.makeText(MainActivity.this, "進來了stop", Toast.LENGTH_SHORT).show();
    stopService(intent);
   }
  }
  
 }
}

Service:主要是響應點擊事件以及建立數據庫android

 package com.example.jserver;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
public class JService  extends Service{
 @Override
 public IBinder onBind(Intent intent) {
  System.out.println("------>onBind");
  return null;
 }
 @Override
 public void onCreate() {
  super.onCreate();
  System.out.println("------>onCreate");
 }
 @Override
 public void onDestroy() {
  System.out.println("------>onDestroy");
  super.onDestroy();
 }
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  System.out.println("------>onStartCommand");
  JLite lite=new JLite(JService.this, "ser_user", null, 1);
  SQLiteDatabase db=lite.getReadableDatabase();
  lite.onCreate(db);
  for (int i = 10; i < 100; i++) {
   ContentValues values=new ContentValues();
   values.put("name", "呂月秋"+i);
   values.put("address", "東湖"+i);
   db.insert("ser_user", null, values);
  }
  Cursor cursor=db.rawQuery("select * from ser_user", null);
  List<Map<String, String>> list=new ArrayList<Map<String,String>>(); 
  while (cursor.moveToNext()) {
   String id=cursor.getInt(cursor.getColumnIndex("id"))+"";
   String name=cursor.getString(cursor.getColumnIndex("name"));
   String address=cursor.getString(cursor.getColumnIndex("address"));
   System.out.println(id+":"+name+":"+address);
   Map<String, String> map=new HashMap<String, String>();
   map.put("id", id);
   map.put("name", name);
   map.put("address", address);
   list.add(map);
  }
  intent.putExtra("list", (Serializable)list);
  intent.setClass(JService.this, ShowActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  startActivity(intent);
  return super.onStartCommand(intent, flags, startId);
 }
 
}

安卓數據庫:sql

package com.example.jserver;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class JLite  extends SQLiteOpenHelper {
 public JLite(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
  System.out.println("---->createDataBase");
     db.execSQL("drop table IF exists ser_user"); 
  db.execSQL("create table ser_user(id integer primary key autoincrement , name text,address text)");
 }
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  System.out.println("---->onUpgrade");
 }
}

處理須要顯示的Acivity數據庫

package com.example.jserver;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class ShowActivity extends ListActivity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.toshow);
  Intent intent=getIntent();
  List<Map<String, String>> list=(List<Map<String, String>>) intent.getSerializableExtra("list");
  SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.show,  new String[]{"id","name","address"}, new int[]{R.id.id,R.id.name,R.id.address});
  setListAdapter(adapter);
 }
 
}

主要佈局文件man.xml:app

<RelativeLayout 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=".MainActivity" >
   <Button
       android:id="@+id/ser"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="onserver"
       />
   <Button
       android:id="@+id/stopSer"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="stopSer"
       android:layout_below="@id/ser"
       />
</RelativeLayout>

處理LIST數據顯示的佈局文件toshow.xmlide

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
 <ListView 
     android:id="@+id/android:list"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:drawSelectorOnTop="false"
     android:scrollbars="vertical"
     ></ListView>
</LinearLayout>

 顯示數據文件show.xml佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
 <TextView 
     android:id="@+id/id"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="20pt"
     />
 <TextView 
     android:id="@+id/name"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      android:layout_marginLeft="20pt"
     />
 <TextView 
     android:id="@+id/address"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
      android:layout_marginLeft="20pt"
     />
</LinearLayout>

關於Activity的配置文件this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jserver"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.jserver.MainActivity"
            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="com.example.jserver.JService"></service>
        <activity android:name="com.example.jserver.ShowActivity"></activity>
    </application>
</manifest>
相關文章
相關標籤/搜索