zoukankan      html  css  js  c++  java
  • 23 服务的创建Demo1

    结构这里写图片描述

    MainActivity.java

    package com.qf.day23_service_demo1;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
        //Intent intent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
    
        //启动Service
        public void StartServiceClick(View v){
            Intent intent = new Intent(MainActivity.this, MyService.class);
            intent = new Intent();
            intent.setAction("com.qf.day23_service_demo1.MyService");
            //6.0新特性必须设置
            intent.setPackage(getPackageName());
            intent.putExtra("str", "老家肉饼");
            startService(intent);
    
        }
        //停止Service
        public void StopSerViceCliclk(View v){
            Intent intent = new Intent(MainActivity.this, MyService.class);
            intent = new Intent();
            intent.setAction("com.qf.day23_service_demo1.MyService");
            stopService(intent);
        }
    
    }
    

    MyService.java

    package com.qf.day23_service_demo1;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service{
    
        private  static final String TAG ="MyService";
    
        /**
         * 必须实现的方法   绑定服务时调用的方法
         */
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
    
            Log.e(TAG , "===onBind===");
            return null;
        }
    
        /**
         * 表示服务创建时调用
         */
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            Log.e(TAG , "===onCreate===");
        }
    
        /**
         * 通过启动服务 startService() 调用此方法
         * 参数1:启动时传递的对象
         * 参数2:flags  标记
         * 参数3:表示请求的标识
         * 
         * 返回值:
         * START_STICKY:粘性的Service    表示被kill掉  尝试重新去启动  如果服务启动成功会调用此方法    intent 的值null
         * START_NOT_STICKY:非粘性Service   表示被kill掉只能通过StartService()再次启动
         * START_REDELIVER_INTENT:表示被Kill掉  系统自动重启改服务  并将Intent传入
         *
         *
         */
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
    
            String str = intent.getStringExtra("str");
    
            Log.e(TAG , "===onStartCommand==="+str);
    
            return START_NOT_STICKY;
        }
    
    
        /**
         * 解除绑定时 调用此方法
         */
        @Override
        public boolean onUnbind(Intent intent) {
            // TODO Auto-generated method stub
            Log.e(TAG , "===onUnbind===");
            return super.onUnbind(intent);
        }
    
        /**
         * Service销毁时调用此方法
         */
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
    
            Log.e(TAG , "===onDestroy===");
        }
    
    }
    

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.qf.day23_service_demo1"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.qf.day23_service_demo1.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   name="包名.类名"
             -->
            <service android:name=".MyService">
                <intent-filter>
                    <action android:name="com.qf.day23_service_demo1.MyService"/>
                </intent-filter>
            </service>
        </application>
    
    </manifest>
    
  • 相关阅读:
    ubuntu关闭和开启防火墙
    supervisor 从安装到使用
    suse11/12关闭防火墙
    [Linux实用工具]Linux监控工具munin的展示(Nginx)
    [Linux实用工具]Linux监控工具munin的安装和配置
    [Linux]阿里云免费试用体验(在阿里云的ubuntu上部署个人服务)
    [Linux基础环境/软件]Linux下安装resin web服务器(涉及gcc、jdk环境部署)
    [Linux基础环境/软件]Linux下安装mysql
    [Linux实用工具]Windows下同步Linux文件(Linux安装Samba和配置)
    [Linux实用工具]Ubuntu环境下SSH的安装及使用
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152187.html
Copyright © 2011-2022 走看看