Android服务(Service)全解析(五)--前台Service
MainActivity如下:
package cc.testservice2;import android.app.Activity;import android.content.ComponentName;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.TextView;/** * Demo描述: * 前台Service使用示例 * * 主要实现: * 1 在Service的onCreate()中调用startForeground() * 2 在Service的onDestroy()中调用stopForeground() * * 参考资料: * 1 http://blog.csdn.net/guolin_blog/article/details/11952435 * 2 http://blog.csdn.net/think_soft/article/details/7299438 * 3 http://blog.csdn.net/lfdfhl/article/details/10044161 * Thank you very much */public class MainActivity extends Activity { TextView mNumberTextView; TextView mResultTextView; Button mSearchButton; ServiceConnectionImpl mServiceConnectionImpl; QueryInterface mBinder; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //用于接收服务返回的Binder对象 mServiceConnectionImpl=new ServiceConnectionImpl(); mNumberTextView=(TextView) findViewById(R.id.numberEditText); mResultTextView=(TextView) findViewById(R.id.resultTextView); mSearchButton=(Button) findViewById(R.id.searchButton); mSearchButton.setOnClickListener(new ButtonOnClickListener()); Intent intent=new Intent(this,ServiceSubclass.class); //当Activity启动的时候就启动服务 bindService(intent, mServiceConnectionImpl, this.BIND_AUTO_CREATE); } private class ButtonOnClickListener implements OnClickListener{public void onClick(View v) {String number=mNumberTextView.getText().toString();String result=mBinder.queryByNumber(Integer.valueOf(number));mResultTextView.setText(result);} } //绑定服务和解除服务 private final class ServiceConnectionImpl implements ServiceConnection{ //绑定服务时,此方法调用public void onServiceConnected(ComponentName name, IBinder service) { mBinder=(QueryInterface) service;}//解除服务时,此方法调用public void onServiceDisconnected(ComponentName name) {mBinder=null;} } //解除与服务的连接protected void onDestroy() {unbindService(mServiceConnectionImpl);super.onDestroy();} }
ServiceSubclass如下:
package cc.testservice2;import android.app.Notification;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class ServiceSubclass extends Service {private String[] names = new String[] { "小明", "小王", "小杨", "小李", "小强" };BinderSubclass mBinderSubclass = new BinderSubclass();@Overridepublic void onCreate() {super.onCreate();Notification notification=new Notification(R.drawable.ic_launcher, "来了个通知", System.currentTimeMillis());Intent intent=new Intent(this, MainActivity.class);PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0);notification.setLatestEventInfo(this, "通知标题", "通知的内容", pendingIntent);startForeground(9527, notification);System.out.println("在onCreate()中---> startForeground()");}@Overridepublic IBinder onBind(Intent intent) {return mBinderSubclass;}@Overridepublic void onDestroy() {super.onDestroy();stopForeground(true);System.out.println("在onDestroy()中---> stopForeground()");}// queryByNumber就是接口里的业务方法.//一般来讲将业务抽象为一个接口,然后去实现接口,比如此处。// 注意:BinderSubclass继承自Binder也实现了业务接口private final class BinderSubclass extends Binder implements QueryInterface {public String queryByNumber(int number) {return query(number);}}//服务内部的方法public String query(int i) {if (i > 0 && i < 6) {return names[i - 1];}return "查询错误,请再次输入";}}
QueryInterface如下:
package cc.testservice2;//业务接口public interface QueryInterface { public String queryByNumber(int number);}
main.xml如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入1到5的数字" /> <EditText android:id="@+id/numberEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/searchButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" /> <TextView android:id="@+id/resultTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>