TP 4 Intent, IntentFilter et Receiver, une solution

Modérateurs : Douin, F. Lemoine, Farinone

Douin
Messages : 167
Inscription : 18 mars 2009 15:46

TP 4 Intent, IntentFilter et Receiver, une solution

Message par Douin » 22 mars 2017 11:08

TP 4 Intent, IntentFilter et Receiver une solution

Code : Tout sélectionner

package cnam.smb116.tp4;





import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

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.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private final String TAG = this.getClass().getSimpleName();
	private final boolean I = true;

	private Button startBtn, stopBtn;

	private Ticker ticker;
	private boolean stopped;

	private static long count;

	private class Ticker extends Thread implements Serializable, Parcelable{
		public static final String TIME_ACTION_TIC = "time_action_tic";
		public static final String COUNT_EXTRA = "count";
		private Context context;


		public Ticker(Context context){
			this.context = context;
		}
		public void startTicker(){
			this.start();
		}
		public void stopTicker(){
			this.interrupt();
		}
		public void run(){
			Intent intent = new Intent();
			intent.setAction(TIME_ACTION_TIC);
			while(!isInterrupted()){
				SystemClock.sleep(1000L);
				count++;
				intent.putExtra(Ticker.COUNT_EXTRA, count);

				if(count<=10)
					context.sendBroadcast(intent);
				else
					context.sendOrderedBroadcast(intent,null);

			}
		}

		public int describeContents(){
			return 0;
		}
		public void writeToParcel(Parcel dest, int flags){
			dest.writeValue(this); // sauvegarde de la référence
		}
		public  final Parcelable.Creator<Ticker> CREATOR
		= new Parcelable.Creator<Ticker>(){
			public Ticker createFromParcel(Parcel in){
				return new Ticker(in);
			}
			public Ticker[] newArray(int size){
				return new Ticker[size];
			}
		};
		private Ticker(Parcel in){
			MainActivity.this.ticker = (Ticker)in.readValue(MainActivity.class.getClassLoader());
		}
	}

	private Receiver r1,r2,r3;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(I)Log.i(TAG,"onCreate");
		// IHM
		setContentView(R.layout.activity_main);
		this.stopped = true;
		this.startBtn = (Button)findViewById(R.id.startId);
		this.stopBtn = (Button)findViewById(R.id.stopId);

		startBtn.setEnabled(stopped);
		stopBtn.setEnabled(!stopped);


		this.r1 = new Receiver("r1", (TextView)findViewById(R.id.textViewReceiver1Id));
		this.r2 = new Receiver("r2", (TextView)findViewById(R.id.textViewReceiver2Id));
		this.r3 = new Receiver("r3", (TextView)findViewById(R.id.textViewReceiver3Id));

	}
	@Override
	public void onResume(){
		super.onResume();
		if(I)Log.i(TAG,"onResume");

		IntentFilter filter = new IntentFilter();
		filter.addAction(Ticker.TIME_ACTION_TIC);
		filter.setPriority(100);
		registerReceiver(r1, filter);
		filter.setPriority(200);
		registerReceiver(r2, filter);
		filter.setPriority(300);
		registerReceiver(r3, filter);
	}

	@Override
	public void onPause(){
		super.onPause();
		if(I)Log.i(TAG,"onPause");
		unregisterReceiver(r1);
		unregisterReceiver(r2);
		unregisterReceiver(r3);
	}


	@Override
	public void onStop(){
		super.onStop();
		if(I)Log.i(TAG,"onStop");
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		if(I)Log.i(TAG,"onDestroy");
	}

	public void onClickStart(View v){
		if(ticker==null){
			ticker = new Ticker(this);
			ticker.startTicker();
			this.stopped = false;
			startBtn.setEnabled(stopped);
			stopBtn.setEnabled(!stopped);
		}
	}

	public void onClickStop(View v){
		this.stopped = true;
		startBtn.setEnabled(stopped);
		stopBtn.setEnabled(!stopped);
		ticker.stopTicker();
		ticker = null;
	}

	public void onClickFinish(View v){
		if(ticker!=null){
			ticker.stopTicker();
			ticker = null;
		}
		finish(); // onPause -> onStop -> onDestroy
		MainActivity.this.count = 0L;
	}

	//    // Save the thread
	//    @Override
	//    public Object onRetainNonConfigurationInstance() {
	//    	if(I)Log.i(TAG,"onRetainNonConfigurationInstance");
	//        return ticker;
	//    }

	public void onSaveInstanceState(Bundle outState){
		if(I)Log.i(TAG,"onSaveInstanceState");
		//outState.putSerializable("ticker", ticker);
		outState.putParcelable("ticker",ticker);

	}

	public void onRestoreInstanceState(Bundle outState){
		if(I)Log.i(TAG,"onRestoreInstanceState");
		//ticker = (Ticker)outState.getSerializable("ticker");
		ticker = (Ticker)outState.getParcelable("ticker");
		stopped = ticker==null;
		startBtn.setEnabled(stopped);
		stopBtn.setEnabled(!stopped);
	}


	public class Receiver extends BroadcastReceiver{
		private TextView textView;
		private String name;
		public Receiver(String name, TextView textView){
			this.textView = textView;
			this.name = name;
		}
		public void onReceive(Context context, Intent intent) {
			long count = intent.getLongExtra(Ticker.COUNT_EXTRA, 0L);
			textView.setText(name  + ": count: " + count);
			if(isOrderedBroadcast() && name.equals("r2") && count>20){
				abortBroadcast();
			}

		}
	}

}


Répondre

Qui est en ligne ?

Utilisateurs parcourant ce forum : Aucun utilisateur inscrit et 1 invité