Let’s begin with secured Broadcast intent handling. We are going to use the theories we have discussed on the prelab section to secure the intent’s from malicious application. To begin go to your Android Studio and create a new project name it “InterAppSender” see the image below
File->New-> New Project->Select Basic Activity->Click Next
Create a new Android Studio project and name it “InterAppSender” with the company domain of "com.example" and click on Next
Copy and paste the following code into “MainActivity.java” and "Other Pages"
//Copy and Paste following code in MainActivity.java
//MainActivity.java
package com.example.interappsender;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText name, password;
private Context context;
public MainActivity(){
this.context = MainActivity.this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
password = findViewById(R.id.password);
MainActivity.this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String name = bundle.getString("userName");
String password = bundle.getString("password");
Intent show = new Intent(MainActivity.this, com.example.interappsender.ShowResultOfReceiver.class);
show.putExtra("name", name);
show.putExtra("password", password);
startActivity(show);
}
}, new IntentFilter("com.example.CUSTOM_ACTION"), "example.com.interappsenderv4.permission.CUSTOM_PERMISSION", null);
}
public void sendData(View view) {
int permission = Build.VERSION.SDK_INT>=23 ? ActivityCompat.checkSelfPermission(context,
com.example.interappsender.Manifest.permission.CUSTOM_PERMISSION):
ContextCompat.checkSelfPermission(context,com.example.interappsender.Manifest.permission.CUSTOM_PERMISSION);
if (permission != PackageManager.PERMISSION_DENIED){
CustomDialog dialog = new CustomDialog(context);
String title = "Permission";
String message = "Need to Allow this permission to send Data.";
String positiveButton = "Accept";
String negativeButton = "Deny";
dialog.showDialog(title,message,positiveButton,negativeButton);
dialog.setCustomDialogListener(new AppCustomListener() {
@Override
public void didPressedPositiveButton() {
Bundle bundle = new Bundle();
bundle.putString("userName",name.getText().toString());
bundle.putString("password",password.getText().toString());
Intent intent = new Intent("com.example.CUSTOM_ACTION");
intent.putExtras(bundle);
sendBroadcast(intent,"example.com.interappsender.permission.CUSTOM_PERMISSION");
}
});
}
}
}
//Copy and Paste Following code in AndroidManifest.xml
//AndroidManifest.xml
//Copy and Paste Following code in activity_main.xml
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.interappsender.MainActivity">
<TextView
android:id="@+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="95dp"
android:text="Username"
android:textSize="20dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_below="@+id/name_text"
android:layout_centerHorizontal="true"
android:ems="10"
android:background="#BED2CE"
android:layout_marginTop="5dp"
android:gravity="center"/>
<TextView
android:id="@+id/pass_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/name_text"
android:layout_below="@+id/name"
android:layout_marginTop="19dp"
android:text="Password"
android:textSize="20dp"/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pass_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:ems="10"
android:inputType="textPassword"
android:background="#BED2CE"
android:gravity="center"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/name_text"
android:layout_below="@+id/password"
android:layout_marginTop="10dp"
android:text="Send"
android:onClick="sendData"/>
</RelativeLayout>
Now select Analyze Project Files the select as following