zoukankan      html  css  js  c++  java
  • Android之Service

    1.自定义Service类

    package com.example.mars_2000_service;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    public class FirstService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            System.out.println("Service onBind");
            return null;
        }
    
        // 当创建一个Servcie对象之后,会首先调用这个函数
        @Override
        public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            System.out.println("Service onCreate");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            System.out.println("flags--->" + flags);
            System.out.println("startId--->" + startId);
            System.out.println("Service onStartCommand");
            return START_NOT_STICKY;
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stubo
            System.out.println("Service onDestory");
            super.onDestroy();
        }
    }

    2.启动和关闭Service

    public class MainActivity extends Activity {
        private Button startServiceButton = null;
        private Button stopServiceButton = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            startServiceButton = (Button) findViewById(R.id.startService);
            startServiceButton.setOnClickListener(new StartServiceListener());
            stopServiceButton = (Button) findViewById(R.id.stopService);
            stopServiceButton.setOnClickListener(new StopServiceListener());
            System.out.println("Activity onCreate");
            
            /*if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }*/
        }
    
        class StartServiceListener implements OnClickListener {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, FirstService.class);
                startService(intent);
            }
        }
    
        class StopServiceListener implements OnClickListener {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, FirstService.class);
                stopService(intent);
            }
        }

    3.设置权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.mars_2000_service"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            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.mars_2000_service.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=".FirstService"></service>
        </application>
    
    </manifest>
  • 相关阅读:
    mysql 应用 持续更新2 转载
    sql server 用触发器记录增删改操作(转载)
    mysql 应用 持续更新
    oracle 常用指令(持续更新中....)
    转载-Oracle 数据库导入导出 dmp文件
    Web Service 服务无法连接Oracle数据库
    关于jquery获取服务器端xml数据
    Navicat Premium 自动备份mysql和sqlserver
    浅谈如何更好的打开和关闭ADO.NET连接池
    JSON 的优点
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3650292.html
Copyright © 2011-2022 走看看