1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| package fr.nat.servicetestapp;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Parcel;
import android.os.ResultReceiver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import fr.nat.servicetestapp.TestReceiver;
public class MainActivity extends AppCompatActivity {
private static final String EXTRA_PARAM1 = "fr.nat.siboxpasserelle.services.extra.PARAM1";
private static final String EXTRA_PARAM2 = "fr.nat.siboxpasserelle.services.extra.PARAM2";
private static final String ACTION_FOO = "fr.nat.siboxpasserelle.services.action.FOO";
private static final String ACTION_BAZ = "fr.nat.siboxpasserelle.services.action.BAZ";
public TestReceiver receiverForTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupServiceReceiver();
Button button_baz = (Button) findViewById(R.id.button_baz);
button_baz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on lance le service
Intent msgIntent = new Intent(Intent.ACTION_MAIN);
msgIntent.putExtra(EXTRA_PARAM1,"BONJOUR");
msgIntent.putExtra(EXTRA_PARAM2,"BONSOIR");
msgIntent.setAction(ACTION_BAZ);
msgIntent.setComponent(new ComponentName("fr.nat.siboxpasserelle","fr.nat.siboxpasserelle.services.MyIntentService"));
msgIntent.putExtra("receiver", receiverForSending(receiverForTest));
startService(msgIntent);
}
});
Button button_foo = (Button) findViewById(R.id.button_foo);
button_foo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on lance le service
Intent msgIntent = new Intent(Intent.ACTION_MAIN);
msgIntent.putExtra(EXTRA_PARAM1,"GOODMORNING");
msgIntent.putExtra(EXTRA_PARAM2,"GOODNIGHT");
msgIntent.setAction(ACTION_FOO);
msgIntent.putExtra("receiver", receiverForSending(receiverForTest));
msgIntent.setComponent(new ComponentName("fr.nat.siboxpasserelle","fr.nat.siboxpasserelle.services.MyIntentService"));
startService(msgIntent);
}
});
}
// Setup the callback for when data is received from the service
public void setupServiceReceiver() {
receiverForTest = new TestReceiver(new Handler());
// This is where we specify what happens when data is received from the service
receiverForTest.setReceiver(new TestReceiver.Receiver() {
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
if (resultCode == RESULT_OK) {
String resultValue = resultData.getString("resultValue");
Toast.makeText(MainActivity.this, resultValue, Toast.LENGTH_SHORT).show();
}
}
});
}
public static ResultReceiver receiverForSending(ResultReceiver actualReceiver) {
Parcel parcel = Parcel.obtain();
actualReceiver.writeToParcel(parcel,0);
parcel.setDataPosition(0);
ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
parcel.recycle();
return receiverForSending;
}
} |
Partager