• Android它Service


    服务是一段代码的后台执行。

    无法处理,也不是螺纹,但它是在进程和线程的执行。

    Android该服务与Activity不同,不能与用户交互,无法启动自己。

    媒体播放服务。当用户退出媒体选择用户界面,不过我希望音乐依然能够继续打,这是服务Service来保证当用户界面关闭时音乐继续播放。当我们某个应用的数据是通过网络获取的,不同一时候间的数据是不同的,这时我们能够用Service在后台定时更新。而不用每次打开应用的时候去获取。

    实例:ServiceDemo
    执行效果:


    代码清单:
    AndroidManifest.xml

    <?

    xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rainsong.servicedemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <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=".MyService" /> </application> </manifest>


    布局文件:main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <Button android:id="@+id/btn_start"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Start Service" />
        <Button android:id="@+id/btn_stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Stop Service" />
    </LinearLayout>
    

    Java源码文件:MainActivity.java
    package com.rainsong.servicedemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.content.Intent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity
    {
        Button btn_start;
        Button btn_stop;
        
        OnClickListener listener_start;
        OnClickListener listener_stop;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            listener_start = new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, MyService.class);
                    startService(intent);
                }
            };
            listener_stop = new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, MyService.class);
                    stopService(intent);
                }
            };
    
            btn_start = (Button)findViewById(R.id.btn_start);
            btn_start.setOnClickListener(listener_start);
    
            btn_stop = (Button)findViewById(R.id.btn_stop);
            btn_stop.setOnClickListener(listener_stop);
        }
    }

    Java源码文件:MyService.java

    package com.rainsong.servicedemo;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.Binder;
    import android.os.IBinder;
    import android.widget.Toast;
    import android.util.Log;
    
    public class MyService extends Service {
        @Override
        public IBinder onBind(Intent i) {
            return null;
        }
    
        @Override
        public boolean onUnbind(Intent i) {
            return false;
        }
    
        @Override
        public void onCreate() {
            Toast.makeText(this, "Service onCreate", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onStart(Intent i, int startId) {
            Toast.makeText(this, "Service onStart", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onDestroy() {
            Toast.makeText(this, "Service  onDestroy", Toast.LENGTH_SHORT).show();
        }
    
    }
    

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Linux
    Cookie & Session
    HTTP
    HTTP
    抓包工具
    抓包工具
    抓包工具
    python
    python
    python
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4633120.html
Copyright © 2020-2023  润新知