I ask if I can pass to an other application some data using intent. If it's possible, how can I do clicking a button and passing to an other application?
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intention = new Intent(?????);
startActivity(intention);
}
});
To pass data from one activity to another you need to add it to the Intent. E.g,
Intent intention = new Intent(this, DestClass.class);
int value = 10;
intention.putExtra("KEY", value);
startActivity(intention);
and in your DestClass's onCreate(), you get it from the Intent with,
Bundle extras = getIntent().getExtras();
if (extras!= null) {
extras.getInt("KEY");
}
To send it to another application. Similarly you create an Intent as shown in the Android docs.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Note that in this case the Android system allows apps to register to receive Intents and if there are multiple apps that have registered for these Intents then the system lets the user choose which App they would like to handle the Intent.
Related
In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 4 years ago.
I am very new to Android Studio, and have found myself stuck in this concept. I am attempting to pass Price + Name data to a Cart activity upon a button press (Add to Cart).
After attempting intent methods, it seems that after pressing "Add to Cart", the cart is opened with the data, but the data is not saved in the new activity for more additions.
Right now I have the following:
Button button = (Button) findViewById(R.id.addcart);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//How to pass information here
}
});
I am hoping to pass textView6 and textView7 to the cart activity. If possible, I would be interested in passing the image as well! Any start on this would be appreciated. Thank you!
To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:
Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);
And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent data = getIntent();
String price = data.getStringExtra("price");
String name = data.getStringExtra("name");
}
Hope this help.
When you create your Intent you have to do :
Intent i = new Intent(this, ToClass.class);
i.putExtra("someName", findViewById(R.id.textView6 ).getText().toString());
i.putExtra("someName2", findViewById(R.id.textView7 ).getText().toString());
startActivity(i);
And then in your second Activity, use :
Intent intent = getIntent();
String someName= intent.getStringExtra("someName");
String someName2= intent.getStringExtra("someName2");
You say your using an intent, but what are you doing with the values? Are you saving them somewhere in the CartActivity?
For the image just pass a reference or name of the image. No need to pass the whole PNG... and again use an intent putExtra() call.
In my app i have a button to select a contact from contacts phone and a button to start a call phone to this number. So when i click on the button to select the contact, the complete action using dialog appears with more apps to choose as well as when i click on the button to star the call phone. How can i avoid the dialog to access contacts and to do a call phone directly?
Partial code of my activity:
contacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
}
});
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String numeroDiTelefono = dati.getString("numeroDiTelefono");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + numeroDiTelefono));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}
});
Make sure you have added the permission in the Manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
And all you should need for the Intent is:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+numeroDiTelefono));
startActivity(callIntent);
Basically that dialog means you have more than one contacts app installed on your phone. This is a default Android system behavior when you call any kind of common intent actions.
What you can do is make the intent more specific to the app you're looking for.
By specifing
a) the specific data uri
b) the package name
c) set the content type, etc
Also try this.
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
public void onClick(View view) {
String number = String.valueOf(bundle.getLong("phone"));
Uri call = Uri.parse("tel:" + number);
Intent intent = new Intent(Intent.ACTION_DIAL, call);
startActivity(intent);
}
In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(i);
}
}
);
I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.
What is causing my app to crash?
Change your onClick method to below code. You should give the option to choose the external player.
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
Change your code to add this check:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
// Check there is an activity that can handle this intent
if (i.resolveActivity(getPackageManager()) == null) {
// TODO No activity available. Do something else.
} else {
startActivity(i);
}
I am making an Android application containing more than 60 buttons. Each button responds to an Activity. Each Activity contains a sound file sourced from a raw file, a TextView and an image. Is there any way that I can use Intent parameters for each button?
For example:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile", "FirstKeyValue");
myIntent.putExtra("image", "SecondKeyValue");
myIntent.putExtra("text", "ThirdKeyValue");
startActivity(myIntent);
//Make method let suppose
public void sendMysong(String songname,String imgUrl,String text)
{
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile",songname);
myIntent.putExtra("image",imgUrl);
myIntent.putExtra("text",text);
}
// Now in receiving Activity receive intent data
Bundle extras = getIntent().getExtras();
if (extras != null) {
String fname=extras.get("soundfile");
int resID=getResources().getIdentifier(fname, "raw", getApplicationContext().getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
}
Sixty button in single layout is not a good design pattern, you must use either grid or List view.
Using List View
http://developer.android.com/design/building-blocks/lists.html
Using Grid View
http://developer.android.com/guide/topics/ui/layout/gridview.html
As far as the current problem is concerned
You can create a generalized Function as given below
public boolean startActivityFoo(String value1,String value2,String value3)
{
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("soundfile",value1);
myIntent.putExtra("image",value2);
myIntent.putExtra("text",value3);
startActivity(myIntent);
}