Created
November 5, 2019 13:03
-
-
Save MahmoudMabrok/b4c50bc53e5dc9b6fbc13ea0b9669ac8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
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>
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
server broadcast receiver :