2015年3月9日 星期一

Android-Service 藉由 Broadcast 將變數傳至 Activity

之前有講到如何執行Service服務
但是您在背景的東西要怎麼丟到前台?
好比如你請郵差幫你寄信,卻沒寫地址!
那不是無功而返嗎?

這次利用Service內去註冊廣播的服務,請Activity接收訊息。
Activity(Button)
Activity(StartService)
Service(While Loop - Broadcast)
Activity( BroadcastReceiver )

MainActivity.java
package com.example.service;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    private Button startBtn;
    private Button stoptBtn;
    private TextView numText;
    private MyReceiver receiver;

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

        startBtn = (Button) findViewById(R.id.button1);
        stoptBtn = (Button) findViewById(R.id.button2);
        numText = (TextView) findViewById(R.id.textView1);

        startBtn.setOnClickListener(this);
        stoptBtn.setOnClickListener(this);

        // 註冊接收器
        receiver=new MyReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("android.intent.action.test");
        this.registerReceiver(receiver,filter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 結束背景服務
        Intent stopIntent = new Intent(this, Swe.class);
        stopService(stopIntent);
        // 註銷接收廣播服務
        this.unregisterReceiver(receiver);
    }

    @Override
    public void onClick(View view) {

        switch(view.getId()){

        case R.id.button1:

            Intent intent  = new Intent(this,Swe.class);
            startService(intent);
            break;

        case R.id.button2:

            Intent stopIntent = new Intent(this, Swe.class);
            stopService(stopIntent);
            break;

        }
    }

    public class MyReceiver extends BroadcastReceiver {
        
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle=intent.getExtras();
            String a=bundle.getString("Swe_sum");
            Log.d("ServiceProject","MainActivity Receiver : "+a);
            numText.setText(a);

        }
    }
}


Swe.java
package com.example.service;

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

public class Swe extends Service{

    private int sum = 0;
    private boolean running = true;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("ServiceProject","onCreate");
        thread.start();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("ServiceProject","onDestroy");
        running = false;

    }

    Thread thread = new Thread(new Runnable(){

        @Override
        public void run() {
            while(running){
                sum++;
                Log.d("ServiceProject","ServiceSwe : "+sum);
                Intent intent=new Intent();
                intent.putExtra("Swe_sum", ""+sum);
                intent.setAction("android.intent.action.test");
                sendBroadcast(intent);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    running = false;
                    e.printStackTrace();
                }
            }
        }});

}


記得要註冊 Service 、Receiver
<service android:name="Swe"></service>
<receiver android:name="MainActivity$MyReceiver"></receiver>















歡迎轉載,請註明出處。

沒有留言:

張貼留言