赵雅智:service_startService生命周期


案例演示

布局文件

<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"
    tools:context="${packageName}.${activityClass}" >

     <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="context.startService" />


    <Button
        android:id="@+id/btn_stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_start"
        android:onClick="click"
        android:text="context.strpService" />


</RelativeLayout>


注册service
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android_servicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android_servicedemo.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="ServiceTese"
            android:permission="com.example.permission.servers" >
            <intent-filter>
                <action android:name="com.example.serversdemo.action" />
            </intent-filter>
        </service>
    </application>

</manifest>

主activity
package com.example.android_servicedemo;


import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {
	private Intent service;
	MyConn myConn;
	public static final String TAG = "MainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 隐士意图的启动
		service = new Intent();
		service.setAction("com.example.serversdemo.action");
	}

	public void click(View v) {
		int id = v.getId();
		switch (id) {
		/*
		 * 启动服务
		 */
		case R.id.btn_start:
			this.startService(service);
			break;
		/*
		 * 停止服务
		 */
		case R.id.btn_stop:
			this.stopService(service);
			break;
		default:
			break;
		}
	}

}


运行结果
点击context.startService--->点击context.stopService。生命周期为

点击context.startService--->home键--->点击context.startService--->点击context.startService...--->点击context.stopService



onbindService见http://blog.csdn.net/zhaoyazhi2129/article/details/32712073

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wsl2下启用kde出现kf.modemmanagerqt: Failed enumerating MM objects: "org.freedesktop.DBus.Error.TimedOut" "Failed to activate service 'org.freedesktop.ModemManager1': timed out (service_start_timeout=25000ms)" kscreen.kded: PowerDevil SuspendSession action not available! kf.bluezqt: PendingCall Error: "Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken." QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root' QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root' QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root' wsl2下启用kde出现kf.modemmanagerqt: Failed enumerating MM objects: "org.freedesktop.DBus.Error.TimedOut" "Failed to activate service 'org.freedesktop.ModemManager1': timed out (service_start_timeout=25000ms)" kscreen.kded: PowerDevil SuspendSession action not available! kf.bluezqt: PendingCall Error: "Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken." QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root' QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root' QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-ro
最新发布
07-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值