Android Service 生命周期
2016-06-13 23:54
357 查看
package com.example.metrox.l14; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(MainActivity.this,MyService.class); // findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // startService(intent); // } // }); // findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // stopService(intent); // System.out.println("服务已停止!!!"); // } // }); findViewById(R.id.btnStartService).setOnClickListener(this); findViewById(R.id.btnStopService).setOnClickListener(this); findViewById(R.id.btnBindService).setOnClickListener(this); findViewById(R.id.btnUnBindService).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnStartService: startService(intent); break; case R.id.btnStopService: stopService(intent); System.out.println("服务已停止!!!"); break; case R.id.btnBindService: bindService(intent,this,BIND_AUTO_CREATE); break; case R.id.btnUnBindService: unbindService(this); break; } } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("服务已连接..."); } @Override public void onServiceDisconnected(ComponentName componentName) { System.out.println("服务已断开..."); } }
package com.example.metrox.l14; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class MyService extends Service { private boolean isRuning = false; public MyService() { } @Override public IBinder onBind(Intent intent) { return new Binder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("OnStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); System.out.println("Service OnCreate"); isRuning = true; new Thread(){ @Override public void run() { super.run(); while (isRuning){ System.out.println("服务正在运行中..."); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }} } }.start(); } @Override public void onDestroy() { super.onDestroy(); System.out.println("Service Destroy"); isRuning = false; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.metrox.l14.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动服务" android:id="@+id/btnStartService" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止服务" android:id="@+id/btnStopService" android:layout_below="@+id/btnStartService" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="54dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定服务" android:id="@+id/btnBindService" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解绑服务" android:id="@+id/btnUnBindService" android:layout_below="@+id/btnBindService" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="70dp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.metrox.l14"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" android:enabled="true" android:exported="true"></service> </application> </manifest>
相关文章推荐
- Android Scroll分析(一)
- 输入控制——Buttons——翻译自developer.android.com
- LayoutInflater.from(this) 这个this是什么,加载完后的View 就是谁的。
- Android APK安装后点击[打开]与[完成]的区别
- 构建android上多媒体apk
- android.support.design.widget.TabLayout
- Android Volley完全解析
- ionic框架对Android返回键的处理
- Android初级教程理论知识(第五章页面跳转和数据传递)
- Android初级教程理论知识(第五章页面跳转和数据传递)
- android /system/vold源码分析(7)
- Android中的性能优化
- Android 视频通话 AnyChat
- 用Androidstudio的Terminal安装卸载应用
- Android模拟器访问本地的localhost失败及解决方案
- Android自定义控件实战——水流波动效果的实现WaveView
- 改变窗口所在的进程:android:process属性解析
- Android静默安装
- ListView的点击事件
- Android简单计算器