六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 58|回复: 0

Services的进一步测试(使用方法)

[复制链接]

升级  35.2%

212

主题

212

主题

212

主题

进士

Rank: 4

积分
676
 楼主| 发表于 2013-1-15 02:19:57 | 显示全部楼层 |阅读模式
1.界面操作类:
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.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class TestServiceHolder extends Activity {private boolean _isBound;private TestService _boundService;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);setTitle("Service Test");initButtons();}    private ServiceConnection _connection = new ServiceConnection() {          public void onServiceConnected(ComponentName className, IBinder service) {                       _boundService = ((TestService.LocalBinder)service).getService();                            Toast.makeText(TestServiceHolder.this, "Service connected",                      Toast.LENGTH_SHORT).show();          }            public void onServiceDisconnected(ComponentName className) {              // unexpectedly disconnected,we should never see this happen.              _boundService = null;              Toast.makeText(TestServiceHolder.this, "Service connected",                      Toast.LENGTH_SHORT).show();          }      };          private void initButtons() {          Button buttonStart = (Button) findViewById(R.id.start_service);          buttonStart.setOnClickListener(new OnClickListener() {              public void onClick(View arg0) {                  startService();              }          });            Button buttonStop = (Button) findViewById(R.id.stop_service);          buttonStop.setOnClickListener(new OnClickListener() {              public void onClick(View arg0) {                  stopService();              }          });            Button buttonBind = (Button) findViewById(R.id.bind_service);          buttonBind.setOnClickListener(new OnClickListener() {              public void onClick(View arg0) {                  bindService();              }          });            Button buttonUnbind = (Button) findViewById(R.id.unbind_service);          buttonUnbind.setOnClickListener(new OnClickListener() {              public void onClick(View arg0) {                  unbindService();              }          });      }        private void startService() {          Intent i = new Intent(this, TestService.class);          this.startService(i);      }          private void stopService() {          Intent i = new Intent(this, TestService.class);          this.stopService(i);      }    private void bindService() {//调用onCreate(),onBind()方法Intent i = new Intent(this, TestService.class);bindService(i, _connection, Context.BIND_AUTO_CREATE);_isBound = true;}      private void unbindService() {          if (_isBound) {              unbindService(_connection);              _isBound = false;          }      }  }

2.服务类:
import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class TestService extends Service {private static final String TAG = "TestService";private NotificationManager _nm;@Overridepublic IBinder onBind(Intent i) {Log.e(TAG, "============> TestService.onBind");return null;}public class LocalBinder extends Binder {TestService getService() {return TestService.this;}}@Overridepublic boolean onUnbind(Intent i) {Log.e(TAG, "============> TestService.onUnbind");return false;}@Overridepublic void onRebind(Intent i) {Log.e(TAG, "============> TestService.onRebind");}@Overridepublic void onCreate() {// 服务第一次被启动时调用Log.e(TAG, "============> TestService.onCreate");_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);showNotification();}@Override//在onCreate()方法后被调用public void onStart(Intent intent, int startId) {Log.e(TAG, "============> TestService.onStart");}@Overridepublic void onDestroy() {//停止服务的时候被调用_nm.cancel(R.string.service_started);Log.e(TAG, "============> TestService.onDestroy");}private void showNotification() {Notification notification = new Notification(R.drawable.face_1,"Service started", System.currentTimeMillis());PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, TestServiceHolder.class), 0);// must set this for content view, or will throw a exceptionnotification.setLatestEventInfo(this, "Test Service","Service started", contentIntent);_nm.notify(R.string.service_started, notification);}}

3.配置文件:
<application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".TestServiceHolder" 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:enabled="true" android:name=".TestService"android:process=":remote" /></application>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表