启动Service--onstartService实例
2015-11-18 16:38
363 查看
package com.example.services; 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; /** * @author HD * @date 2015-11-18 * @package_name com.example.services * @file_name MainActivity.java */ public class MainActivity extends Activity implements OnClickListener{ private Button btnOpen; private Button btnCancel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOpen = (Button) findViewById(R.id.btnOpen); btnCancel = (Button) findViewById(R.id.btnCancel); btnOpen.setOnClickListener(this); btnCancel.setOnClickListener(this); } @Override public void onClick(View v) { // TODO 自动生成的方法存根 switch (v.getId()) { case R.id.btnOpen: Intent intent = new Intent(MainActivity.this, MyServices.class); intent.putExtra("name", "HD"); startService(intent); break; case R.id.btnCancel: Intent intent2 = new Intent(MainActivity.this, MyServices.class); stopService(intent2); break; default: break; } } }
package com.example.services; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; /** * @author HD * @date 2015-11-18 * @package_name com.example.services * @file_name MyServices.java */ public class MyServices extends Service { private final static String TAG = "MyServices"; public MyServices() { // TODO 自动生成的构造函数存根 } @Override public void onCreate() { // TODO 自动生成的方法存根 super.onCreate(); Log.i(TAG, "------>>onCreate"); } @Override public IBinder onBind(Intent intent) { // TODO 自动生成的方法存根 Log.i(TAG, "------>>IBinder"); return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO 自动生成的方法存根 Log.i(TAG, "------>>onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO 自动生成的方法存根 super.onDestroy(); Log.i(TAG, "------>>onDestroy"); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.services" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="21" /> <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=".MyServices"></service> </application> </manifest>
<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.services.MainActivity" > <Button android:id="@+id/btnOpen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:text="启动服务" /> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="42dp" android:text="结束服务" /> </RelativeLayout>
相关文章推荐
- linux udp 客户端recvfrom,一直没有数据接收,谁能帮看看这代码哪里出了问题了????
- 延迟渲染实现
- Heritrix3.3.0源码阅读 动态加载种子
- java的File.separator
- 函数执行效率测试(foreach、for、while)
- Android中View绘制流程以及invalidate()等相关方法分析
- WPF中使用RenderTransformOrigin来控制动画的起点
- codeforces 405 C. Unusual Product and E. Graph Cutting (异或规律 & 搜索)
- 剑指offer--重建二叉树[根据二叉树的先序、中序遍历结果重建二叉树]
- Windows无法连接到Group Policy Client服务
- Java中的Timer和TimerTask在Android中的用法
- Android Https相关完全解析 当OkHttp遇到Https
- 【HEVC学习与研究】8.关于GOP与RAP的一些解释
- jquery.validate.min.js 用法方法示例
- Lucene查询语法详解
- 前端开发学习笔记(二)JavaScript DOM编程技术(一、二)
- Sigar使用
- iOS中线程Call Stack的捕获和解析(二)
- 【FAQ】How to run sudo command without typing passwd?
- Python处理Json报文