android--Service1(service不是一个单独的进程,也不是一个线程)
2011-04-09 21:36
537 查看
TestActivity.java
FirstService.java
main.xml
mainfest.xml
package zlj.service1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestActivity extends Activity { /** Called when the activity is first created. */ private Button startService = null; private Button stopService = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startService = (Button)findViewById(R.id.startService); startService.setOnClickListener(new startServiceListener()); stopService = (Button)findViewById(R.id.stopService); stopService.setOnClickListener(new stopServiceListener()); } class startServiceListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(TestActivity.this,FirstService.class); startService(intent); } } class stopServiceListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(TestActivity.this,FirstService.class); stopService(intent); } } }
FirstService.java
package zlj.service1; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class FirstService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub System.out.println("Service onBind"); return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("Service onCreate"); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("Service onDestroy"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("flag-->" + flags); System.out.println("startId-->" + startId); System.out.println("Service onStartCommand"); return START_NOT_STICKY; } }
main.xml
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/startService" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="startService" /> <Button android:id="@+id/stopService" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="stopService" /> </LinearLayout>
mainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="zlj.service1" 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=".TestActivity" 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=".FirstService"></service> </application> </manifest>
相关文章推荐
- http://flyvenus.net/ 深入android http://www.eoeandroid.com/thread-67739-1-1.html 从进程-线程的角度,理解Activity,Service,Broadcast - 4.7更
- Android 绑定远程Service(此Service开启在另外一个进程中)
- Android中进程和线程解析,不是想象的那样
- Android的Activity启动长时间的操作开启一个Service比开启一个线程好
- Android 通过Service单独进程模仿离线推送 Server Push
- 在进程或者线程中开启一个emptyService延长生命周期
- Android中Service与Activity的有关于进程与线程的问题
- Android 通过Service单独进程模仿离线推送 Server Push
- 【我的问题】android:service里的一个内部线程类可以公用service的数据吗
- ------------------Android 通过Service单独进程模仿离线推送 Server Push
- Android 通过Service单独进程模仿离线推送 Server Push
- Android 通过Service 单独进程模仿离线推送
- Android 通过Service单独进程模仿离线推送 Server Push
- Android 通过Service单独进程模仿离线推送 Server Push
- android中activity和service是否在同一个进程中
- Android 通过Service单独进程模仿离线推送 Server Push
- UI主线程里单独启一个子线程 -android
- android 进程/线程管理(三)----Thread,Looper / HandlerThread / IntentService
- Android 通过Service单独进程模仿离线推送 Server Push
- Android中Service与Activity的有关于进程与线程的问题