Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.example.firstapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
EditText editText ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private boolean isPackageInstalled(String packageName, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
public void reject(View view) {
final Intent intent=new Intent();
intent.setAction("com.example.secondapp");
intent.putExtra("action","reject");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); // to insure app even if it not running
intent.setComponent(
new ComponentName("com.example.secondapp","com.example.secondapp.MyReceiver"));
sendBroadcast(intent);
}
public void accept(View view) {
if(isPackageInstalled("com.example.secondapp" , getPackageManager()))
{
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.secondapp","com.example.secondapp.MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), " app not installed ", Toast.LENGTH_SHORT).show();
}
}
}
@MahmoudMabrok

This comment has been minimized.

Copy link
Owner Author

@MahmoudMabrok MahmoudMabrok commented Nov 5, 2019

server broadcast receiver :

package com.example.firstapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = "I received from Client " + intent.getStringExtra("action");
        Toast.makeText(context, action, Toast.LENGTH_SHORT).show();
    }
}

@MahmoudMabrok

This comment has been minimized.

Copy link
Owner Author

@MahmoudMabrok MahmoudMabrok commented Nov 5, 2019

Manifest file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <receiver
            android:name=".MyReciever"
            android:enabled="true"
            android:exported="true">

        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
@MahmoudMabrok

This comment has been minimized.

Copy link
Owner Author

@MahmoudMabrok MahmoudMabrok commented Nov 5, 2019

Notes

  • first app represent server code
  • second app represent client app
  • second app is similar to first app.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment