Android应用软件开发

194课时
1.7K学过
8分

课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
3分钟

4.2.3 实施步骤

实施步骤

步骤1:新建项目,命名为IntentService。

步骤2:新建一个Service,如表4-2-9 intentServiceTest.java所示,实现了一个继承了IntentService 的类intentServiceTest,并在各个方法中插入LOG语句,显示调试信息。

表4-2-9 intentServiceTest.java文件清单

package com.example.intentservice;

import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class intentServiceTest extends IntentService {
final String TAG="chen";

    public intentServiceTest() {
        super("intentServiceTest");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: ");
        return super.onBind(intent);
    }


    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate: ");
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        Log.e(TAG, "onStart: " );
        super.onStart(intent, startId);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void setIntentRedelivery(boolean enabled) {
        super.setIntentRedelivery(enabled);
        Log.e(TAG, "setIntentRedelivery: " );
    }

    @Override
    protected void onHandleIntent(Intent intent) {
      //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务
        String action = intent.getExtras().getString("param");
            if (action.equals("oper1")) {
                Log.e(TAG, "Operation1");
            }else if (action.equals("oper2")) {
                Log.e(TAG, "Operation2 " );
            }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy: ");
        super.onDestroy();
    }


}

步骤3:修改MainActivity.java,启动intentServiceTest类。如表表4-2-10所示。

表4-2-10 MainActivity.java文件清单

package com.example.intentservice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void onClick(View view) {
        //Operation 1
        Intent startServiceIntent = new Intent(this, intentServiceTest.class);;
        Bundle bundle = new Bundle();
        bundle.putString("param", "oper1");
        startServiceIntent.putExtras(bundle);
        startService(startServiceIntent);

        //Operation 2
        Intent startServiceIntent2 = new Intent(this, intentServiceTest.class);
        Bundle bundle2 = new Bundle();
        bundle2.putString("param", "oper2");
        startServiceIntent2.putExtras(bundle2);
        startService(startServiceIntent2);
    }
}

步骤4:修改布局文件activity_main.xml,实现一个按钮并设定了点击动作执行的方法。如表4-2-11所示。

表4-2-11 activity_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/intentService"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="启动服务"
        />

</LinearLayout>

步骤5:在AndroidManifest.xml中注册IntentService,增加表4-2-12所示的语句。

表4-2-12 AndroidManifest.xml中增加的语句

<service android:name=".intentServiceTest">     </service>

步骤6:在手机上运行并观察效果。