android_service_totoal

来源:互联网 发布:操作系统linux移植 编辑:程序博客网 时间:2024/06/18 14:42

直接贴代码吧:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jsd.service"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ServiceMain"
                  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=".jsd.ServiceJsd"/>
  <service android:name=".jsd.BindService"/>
  <service android:name=".jsd.MyService"/>
  <service android:name=".jsd.ExampleIntentService"/>
  <service android:name=".jsd.StartBarService"/>
    </application>
</manifest>

 

<?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"
    >
<Button 
 android:id="@+id/startService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="startService"
    />
   <Button
    android:id="@+id/endService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="endService"
    />
   <Button
    android:id="@+id/startBindService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="startBindService"
    /> 
    <Button
    android:id="@+id/endBindService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="endBindService"
    />
     <Button
    android:id="@+id/startMyService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="startMyService"
    /> 
    <Button
    android:id="@+id/endMyService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="endMyService"
    />
    <Button
    android:id="@+id/startIntentService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="startIntentService"
    /> 
    <Button
    android:id="@+id/endIntentService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="endIntentService"
    />
     <Button
    android:id="@+id/startBarIntentService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="startBarIntentService"
    /> 
    <Button
    android:id="@+id/endBarIntentService"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="endBarIntentService"
    />    
</LinearLayout>

package com.jsd.service;

import android.app.Activity;
import android.app.NotificationManager;
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 com.jsd.service.jsd.BindService;
import com.jsd.service.jsd.ExampleIntentService;
import com.jsd.service.jsd.MyService;
import com.jsd.service.jsd.ServiceJsd;
import com.jsd.service.jsd.StartBarService;
import com.jsd.service.jsd.BindService.MyBinder;

/**
 *  main
 * @author jiangshide
 *
 */
public class ServiceMain extends Activity {

 protected static final String TAG = "ServiceMain";
 private Button startService;
 private Button endService;
 private Button startBindService;
 private Button endBindService;
 private Button startMyService;
 private Button endMyService;
 private Button startIntentService;
 private Button endIntentService;
 private Button startBarIntentService;
 private Button endBarIntentService;
 
 private boolean isConnected;
 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getViewById();
    }
 
 public void getViewById(){
  startService = (Button) this.findViewById(R.id.startService);
  endService = (Button) this.findViewById(R.id.endService);
  startBindService = (Button) this.findViewById(R.id.startBindService);
  endBindService = (Button) this.findViewById(R.id.endBindService);
  startMyService = (Button) this.findViewById(R.id.startMyService);
  endMyService = (Button) this.findViewById(R.id.endMyService);
  startIntentService = (Button) this.findViewById(R.id.startIntentService);
  endIntentService = (Button) this.findViewById(R.id.endIntentService);
  startBarIntentService = (Button) this.findViewById(R.id.startBarIntentService);
  endBarIntentService = (Button) this.findViewById(R.id.endBarIntentService);
  
  startService.setOnClickListener(listener);
  endService.setOnClickListener(listener);
  startBindService.setOnClickListener(listener);
  endBindService.setOnClickListener(listener);
  startMyService.setOnClickListener(listener);
  endMyService.setOnClickListener(listener);
  startIntentService.setOnClickListener(listener);
  endIntentService.setOnClickListener(listener);
  startBarIntentService.setOnClickListener(listener);
  endBarIntentService.setOnClickListener(listener);
 }
 
 private OnClickListener listener = new OnClickListener() {
  public void onClick(View v) {
   Intent intent = new Intent();
   intent.setClass(ServiceMain.this, ServiceJsd.class);
   switch (v.getId()) {
   case R.id.startService:
    startService(intent);
    break;
   case R.id.endService:
    stopService(intent);
    break;
   case R.id.startBindService:
    bindService();
    break;
   case R.id.endBindService:
    unBind();
    break;
   case R.id.startMyService:
    intent.setClass(ServiceMain.this, MyService.class);
    Log.i(TAG, "当前主线程ID:"+Thread.currentThread().getId());
    startService(intent);
    break;
   case R.id.endMyService:
    break;
   case R.id.startIntentService:
    intent.setClass(ServiceMain.this, ExampleIntentService.class);
    Log.i(TAG, "主线程ID:"+Thread.currentThread().getId());
    startService(intent);
    break;
   case R.id.endIntentService:
    break;
   case R.id.startBarIntentService:
    intent.setClass(ServiceMain.this, StartBarService.class);
    Log.i(TAG, "主线程ID:"+Thread.currentThread().getId());
    startService(intent);
    break;
   case R.id.endBarIntentService:
    break;
   }
  }

 };
 
 private void unBind() {
  if(isConnected){
   unbindService(conn);
  }
 }

 private void bindService() {
  Intent intent = new Intent();
  intent.setClass(ServiceMain.this, BindService.class);
  bindService(intent, conn,Context.BIND_AUTO_CREATE);
  
 }
 
 private ServiceConnection conn = new ServiceConnection() {
  
  public void onServiceDisconnected(ComponentName name) {
   isConnected = false;
  }
  
  public void onServiceConnected(ComponentName name, IBinder binder) {
   MyBinder myBinder = (MyBinder)binder;
   BindService bindService = myBinder.getService();
   bindService.myMothod();
   isConnected = true;
  }
 };
 
 /**
  * 放进这个方法里是为了能够一直被调用 或者放进 onResume()
  */
 protected void onStart() {
  super.onStart();
  NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  manager.cancel(R.layout.main);
 };
}

 

package com.jsd.service.jsd;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * BindService
 * @author jiangshide
 *
 */
public class BindService extends Service {

 private static final String TAG = "BindService";
 private MyBinder binder = new MyBinder();
 
 public class MyBinder extends Binder{
  
  public BindService getService(){
   return BindService.this;
  }
 }
 
 @Override
 public IBinder onBind(Intent intent) {
  return binder;
 }

 public void myMothod(){
  Log.i(TAG, "myMothod()");
 }
}

/*class MyBinder extends Binder{
 
 public BindService getService(){
  return new BindService();
 }
}*/

package com.jsd.service.jsd;

import com.jsd.service.R;
import com.jsd.service.ServiceMain;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

/**
 * startService
 * @author jiangshide
 *
 */
public class StartBarService extends IntentService {

 protected static final String TAG = "ServiceMain";
// private static final int KUKA = 0;R.layout.main for flag
 public StartBarService() {
  super("StartBarService");
 }
 
 public StartBarService(String name) {
  super(name);
 }

 @Override
 protected void onHandleIntent(Intent intent) {
  Log.i(TAG, "开始下载....");
  showNotification(false);
  Log.i(TAG, "子线程ID:"+Thread.currentThread().getId());
  try {
   Thread.sleep(10000);
   showNotification(true);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  Log.i(TAG, "程序下载完毕!");
 }

 private void showNotification(boolean finish) {
  Notification notification;
  NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  Intent intent = new Intent(this,ServiceMain.class);
  PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  if(!finish){
   notification = new Notification(R.drawable.icon, "开始下载", System.currentTimeMillis());
   
   notification.setLatestEventInfo(this, "下载", "正在下载中", contentIntent);
  }else{
   notification = new Notification(R.drawable.icon, "下载完毕", System.currentTimeMillis());
   notification.setLatestEventInfo(this, "下载", "点击安装", contentIntent);
  }
  notification.defaults = Notification.DEFAULT_ALL;
  manager.notify(R.layout.main, notification);
 }

}

package com.jsd.service.jsd;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 *
 * @author jiangshide
 *
 */
public class ServiceJsd extends Service {

 private static final String TAG = "ServiceJsd";
 
 @Override
 public IBinder onBind(Intent intent) {
  Log.i(TAG, "onBind");
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
  Log.i(TAG, "onCreate");
 }
 
 @Override
 public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  Log.i(TAG, "onStart");
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Log.i(TAG, "onStartCommand");
  return super.onStartCommand(intent, flags, startId);
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
  Log.i(TAG, "onDestroy()");
 }
}

package com.jsd.service.jsd;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 *
 * @author jiangshide
 *
 */
public class MyService extends Service {

 
 private static final String TAG = "ServiceMain";

 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  new MyThread().start();
  return START_STICKY;
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
 }
 
 private class MyThread extends Thread{
  @Override
  public void run() {
   try {
    Thread.sleep(10000);
    Log.i(TAG, "MyService线程ID:"+Thread.currentThread().getId());
    Log.i(TAG, "文件下载...");
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}

package com.jsd.service.jsd;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/**
 *
 * @author jiangshide
 *
 */
public class HandlerService extends Service {

 private static final int COOLSZX = 1;
 private static final int KUKA = 2;
 
 protected static final String TAG = "ServiceMain";
 
 private ServiceHandler handler;
 
 private class ServiceHandler extends Handler{
  public ServiceHandler(Looper looper) {
   super(looper);
  }
  
  @Override
  public void handleMessage(Message msg) {
   switch (msg.what) {
   case COOLSZX:
    Log.i(TAG, "handlerMessage->"+msg.obj);
    break;
   case KUKA:
    Log.i(TAG, "handlerMessage->"+msg.obj);
   default:
    break;
   }
   stopSelf(msg.arg1);
  }
 }
 
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  Log.i(TAG, "HandlerService->onCreate()");
  Looper looper = Looper.getMainLooper();
  handler = new ServiceHandler(looper);
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Log.i(TAG, "HandlerService->onStartCommond()");
  Message msg = handler.obtainMessage();
  msg.what = KUKA;
  msg.arg1 = startId;
  msg.obj = "kuka";
  handler.sendMessage(msg);
  return START_STICKY;
 }
 
 @Override
 public void onDestroy() {
  Log.i(TAG, "HandlerService->onDestory()");
 }
}

package com.jsd.service.jsd;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

/**
 *
 * @author jiangshide
 *
 */
public class ExampleIntentService extends IntentService {

 private static final String TAG = "ServiceMain";

 public ExampleIntentService() {
  super("ExampleIntentService");
 }
 
 public ExampleIntentService(String name) {
  super(name);
 }

 @Override
 protected void onHandleIntent(Intent intent) {
  try {
   Thread.sleep(10000);
   Log.i(TAG, "IntentService线程ID:"+Thread.currentThread().getId());
   Log.i(TAG, "文件下载...");
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

}

上面包含了SERVICE服务的相关处理方式,有STARTSERVICE,BINDSERVICE,与模拟下载通知的案例.......直接仔细看看吧,上面已经是完整代码了

 

原创粉丝点击