Aug 13, 2011

Start a specified app with data passed

In the post Start a specified app, the slave app is satrted without any data passed. Data can be passed from master app to slave app in bundle.

Start a specified app with data passed

Master App:
package com.test.AndroidMaster;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidMasterActivity extends Activity {

final static String PACKAGE_NAME = "com.test.AndroidSlave";
final static String CLASS_NAME = "com.test.AndroidSlave.AndroidSlaveActivity";

EditText textIn;
Button btnStartSlave;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textIn = (EditText)findViewById(R.id.input);
btnStartSlave = (Button)findViewById(R.id.startalave);
btnStartSlave.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String textToBePassed = textIn.getText().toString();
startSlave(textToBePassed);
}});
}

private void startSlave(String passingText){
Intent intent = new Intent();
intent.setClassName(PACKAGE_NAME, CLASS_NAME);

Bundle bundle = new Bundle();
bundle.putString("key", passingText);
intent.putExtras(bundle);

startActivity(intent);
}
}


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startalave"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Slave App"
/>
</LinearLayout>


Slave App:
package com.test.AndroidSlave;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidSlaveActivity extends Activity {

TextView textOut;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textOut = (TextView)findViewById(R.id.output);

Toast.makeText(AndroidSlaveActivity.this,
"onCreate", Toast.LENGTH_LONG).show();

Bundle bundle = this.getIntent().getExtras();

if (bundle==null){
textOut.setText("Self start without bundle");
}else{
String textPassed = bundle.getString("key");

if(textPassed == null){
textOut.setText("No text passed");
}else{
textOut.setText(textPassed);
}
}
}

}


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/output"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>



Related post:
- Check if a app is instlled

No comments:

Post a Comment

Infolinks In Text Ads