service是android开发中的四大组件之一,下面来介绍service的简单用法
1.需要新建一个service类,该类继承与service接口,需要实现onBind方法,这个方法之后介绍
2.创建intent对象,设置intent的目标为新建的service的类,启动service的方法有两种用startservice方法和bindservice方法
两种方法的不同在于startservice在启动服务之后,关闭当前的activity之后service还在系统后台运行
bindservice启动服务后将服务与主界面绑定,主界面关闭之后service也就结束了
3.主界面与service的信息交互需要用到aidl,需要在项目src下新建aidl包,并新建接口,会自动在gen目录下生成对应的java文件,这些东西都是不用进行修改的.
另外,一定记得在androiManifest中注册service
package com.wentjiang.shixi_service; import com.wentjiang.shixi_service.aidl.MyInterface; import android.R.string; import android.app.Activity; import android.app.Service; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ private Button mStartService,mStopService,mBindService,mUnbindService,mAidl; private TextView mTextView; private ServiceConnection mConn; private MyReceiver myreceiver; private MyInterface mInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } private void initView() { mStartService=(Button) findViewById(R.id.startService); mStopService=(Button) findViewById(R.id.stopService); mBindService=(Button) findViewById(R.id.bindService); mUnbindService=(Button) findViewById(R.id.unbindService); mAidl=(Button) findViewById(R.id.aidl); mTextView=(TextView) findViewById(R.id.text); mStartService.setOnClickListener(this); mStopService.setOnClickListener(this); mBindService.setOnClickListener(this); mUnbindService.setOnClickListener(this); mAidl.setOnClickListener(this); } private void initData() { mConn=new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { //连接失败时调用的方法 } @Override public void onServiceConnected(ComponentName name, IBinder service) { //连接成功是调用的方法 Log.i("连接成功了", "---"); mInterface=MyInterface.Stub.asInterface(service); } }; myreceiver=new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("action.went"); registerReceiver(myreceiver, filter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { Intent intent =new Intent(this, MyService.class); switch (v.getId()) { case R.id.startService: startService(intent); break; case R.id.stopService: stopService(intent); break; case R.id.bindService: bindService(intent, mConn, Service.BIND_AUTO_CREATE); break; case R.id.unbindService: unbindService(mConn); break; case R.id.aidl: try { int num= mInterface.getmul(10, 6); mTextView.setText(String.valueOf(num)); } catch (RemoteException e) { e.printStackTrace(); } default: break; } } public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if("action.went".equals(action)){ int num=intent.getIntExtra("num", 0); mTextView.setText(String.valueOf(num)); } } } }
MainActivity
package com.wentjiang.shixi_service; import java.util.Timer; import java.util.TimerTask; import com.wentjiang.shixi_service.MainActivity.MyReceiver; import com.wentjiang.shixi_service.aidl.MyInterface; import android.R.integer; import android.app.Service; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MyService extends Service { private MyReceiver mReceiver; private MyInterface.Stub mBinder = new MyInterface.Stub() { @Override public int getmul(int num1, int num2) throws RemoteException { int temp=num1*num2; return temp; } }; @Override public IBinder onBind(Intent intent) { Log.i("onbind被调用了", "----"); return mBinder; } @Override public void onCreate() { super.onCreate(); Log.i("oncreate被调用了", "----"); } @Override public void onDestroy() { Log.i("onDestroy被调用了", "---"); super.onDestroy(); } @Override public void onRebind(Intent intent) { Log.i("onRebind", "---"); super.onRebind(intent); } }
MyService
package com.wang.service.aidl; interface MyInterface{ int mul(int a, int b); }
MyInterface
<LinearLayout 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:background="#ffffff" android:orientation="vertical" android:padding="20dp" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Test" android:textColor="#000000" android:textSize="24sp" /> <Button android:id="@+id/btn01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/btn02" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service" /> <Button android:id="@+id/btn03" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="bind Service" /> <Button android:id="@+id/btn04" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="unbind Service" /> <Button android:id="@+id/btn05" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="work" /> </LinearLayout>
activity_main
时间: 2024-09-27 01:39:12