服务的生命周期-采用start的方式开启服务
2016-04-27 13:34
204 查看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="start" android:text="开启服务"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="停止服务"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="call" android:text="调用服务里面的方法"/> </LinearLayout>
主Activity:
package com.sql.sericelife; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void start(View v){ Intent intent = new Intent(this,MyService.class); startService(intent);//通知框架开启服务 } public void stop(View v){ Intent intent = new Intent(this,MyService.class); stopService(intent);//通知框架停止服务 } //调用服务里面的方法。不可以自己new服务,调用的服务的方法,必须通过框架得到服务的引用。 public void call(View view){ } @Override protected void onDestroy() { System.out.println("啊啊啊,我是activity,我挂了"); super.onDestroy(); } }
服务类:
package com.sql.sericelife; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service{ @Override public IBinder onBind(Intent intent) { return null; } /** * 开启后服务后,此方法只调用一次 * */ @Override public void onCreate() { System.out.println("oncrete"); super.onCreate(); } /** * 每次调用服务,都会执行的方法 * */ @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onstart"); return super.onStartCommand(intent, flags, startId); } /** * 销毁服务执行的方法 * */ @Override public void onDestroy() { System.out.println("ondestory"); super.onDestroy(); } /** * 这是服务里面的一个方法 */ public void methodInService(){ Toast.makeText(this, "哈哈,服务中自定义的方法", 0).show(); } }
清单文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sql.sericelife" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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="com.sql.sericelife.MyService"></service> </application> </manifest>
相关文章推荐
- Android Studio MultiDex 分包碰到的坑
- 团队冲刺第一阶段个人工作总结9
- scala根据运行环境加载配置文件
- 解析ntfs底层结构
- centos7 安装vnc服务
- 使用Powershell批量获取Exchange 2013邮箱用户容量使用量
- iOS开发-使用Charles进行网络抓包总结
- Android中的二维码生成与扫描
- vector刘汝佳算法入门学习笔记
- 78-对象属性读写
- 77-创建对象实例
- 直接以管理员身份运行bat代码
- 结构体按学号大小输出学生信息
- 多样化条目RecyclerView,以及多样化动画点击效果(附源码)
- DAPM之二: audio paths与dapm kcontrol
- Linux下通过shell批量替换文件内容
- 通过打"patch"简单升级Android Studio
- 电子政务内网安全现状分析与对策
- 乱花渐欲迷人眼——如何看待PMP、CMMI、SLC与项目管理的关系
- 在BB10上增加对Unity游戏的手柄支持