Tuesday, March 16, 2021

Android Share App Data

 

Android Share App Data (ACTION_SEND)

Android uses ACTION_SEND event of android.content.Intent class to send data from one activity to another and from current activity to outside the application. Intent class needs to specify the data and its type which is to be share.

Most commonly, ACTION_SEND action sends URL of build-in Browser app. While sharing the data, Intent callcreateChooser() method which takes Intent object and specify the title of the chooser dialog. Intent.createChooser()method allows to display the chooser.

Example of ACTION_SEND

In this example, we are going to share plain text which is a URL of browser.

activity_main.xml

File: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.test.shareapp.MainActivity">  
  11.   
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="Hello World!"  
  16.         android:id="@+id/textView" />  
  17.   
  18.     <Button  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="Share App"  
  22.         android:id="@+id/button"  
  23.         android:layout_marginBottom="95dp"  
  24.         android:layout_alignParentBottom="true"  
  25.         android:layout_centerHorizontal="true" />  
  26.   
  27. </RelativeLayout>  

Activity class

File: MainActivity.java

  1. package com.example.test.shareapp;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10. Button sharebutton;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.   
  16.         sharebutton=(Button)findViewById(R.id.button);  
  17.         sharebutton.setOnClickListener(new View.OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View v) {  
  20.                 Intent shareIntent =   new Intent(android.content.Intent.ACTION_SEND);  
  21.                 shareIntent.setType("text/plain");  
  22.                 shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Insert Subject here");  
  23.                 String app_url = " https://play.google.com/store/apps/details?id=my.example.javatpoint";  
  24.                 shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,app_url);  
  25.                 startActivity(Intent.createChooser(shareIntent, "Share via"));  
  26.             }  
  27.         });  
  28.     }  
  29. }  

Output

android Share App Data 1
android Share App Data 2 

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile