Library : implementation 'com.mcxiaoke.volley:library:1.0.19'
In 1st Application:
MainActivity.java
package com.example.hemanthkarvy;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
EditText Email, Password;
Button Submit;
ProgressDialog mprocessingdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Email = (EditText) findViewById(R.id.Email);
Password = (EditText) findViewById(R.id.Password);
Submit = (Button) findViewById(R.id.Submit);
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Email.getText().toString().length() > 0) {
if (Password.getText().toString().length() > 0) {
// Intent intent = new Intent(Intent.ACTION_MAIN);
// intent.setComponent(ComponentName.unflattenFromString("com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks"));
// intent.addCategory(Intent.CATEGORY_LAUNCHER);
// startActivity(intent);
// Intent launch_intent = new Intent("android.intent.action.MAIN");
// launch_intent.addCategory("android.intent.category.LAUNCHER");
// launch_intent.setComponent(new ComponentName(packageName, name));
// launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
// startActivity(launch_intent);
// Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.hemanthkarvy2");
// if (launchIntent != null) {
// startActivity(launchIntent);//null pointer check in case package name was not found
// }
// startActivity(new Intent("com.example.hemanthkarvy2.MainActivity"));
new MainActivity.LoginAsync().execute();
} else {
Password.setError("Please Enter Password");
}
} else {
Email.setError("Please Enter Email");
}
}
});
}
private class LoginAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mprocessingdialog = new ProgressDialog(MainActivity.this);
mprocessingdialog.setTitle("Please Wait..");
mprocessingdialog.setMessage("Loading");
mprocessingdialog.setIndeterminate(false);
mprocessingdialog.setCancelable(false);
mprocessingdialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
JSONObject params = new JSONObject();
try {
params.put("authuser", "kfcauser");
params.put("authpass", "UzJGeWRubEFNVEl6Um05eVpYZ2tOVFkzUTNWeWNtVnVZM2tqSkRjNE9VRndjQT09");
params.put("username", Email.getText().toString().trim()); //seenu1233
params.put("password", Password.getText().toString().trim()); //
} catch (Exception e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST, "http://karvyforex.com:8004/CheckLogin", params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("rlog", response.toString());
try {
// JSONObject jsonObj = new JSONObject(response.toString());
//
//
// String message = jsonObj.optString("message");
// String status = jsonObj.optString("status");
//
// if (status.equals("200")) {
//
// }else{
//
// }
if (response.toString().length() > 0) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.hemanthkarvy2");
if (launchIntent != null) {
launchIntent.putExtra("hemanth", response.toString());
startActivity(launchIntent);//null pointer check in case package name was not found
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("rlog", "Error: " + error.getMessage());
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonObjReq);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mprocessingdialog.dismiss();
}
}, 10000);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Email"/>
<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Password"/>
<Button
android:id="@+id/Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Submit"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
In 2nd Application:
MainActivity.java
package com.example.hemanthkarvy2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text =(TextView)findViewById(R.id.text);
Bundle b = getIntent().getExtras();
if(b!=null){
String myString = b.getString("hemanth");
// and any other data that the other app sent
text.setText(myString);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>