-2

It's possible to auto-start android application when a specified event, different by BOOT_COMPLETE, is triggered?

For example i want automatically start my app when a connection is available or when there is an incoming call.

I tried to start my application on SMS received. I'm able to capture the event when my app is running obv but when my app is off the event is not captured and my app not start. Where i wrong?

Manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<receiver
        android:name="com.whatsapp.raj.MyBCast"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </receiver>
</application>

Receiver:

public class MyBCast extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      // TODO: This method is called when the BroadcastReceiver is receiving
      // an Intent broadcast.
      Log.wtf("BCAST", "Connectivity changed!");
      Intent pushIntent = new Intent(context, MainActivity.class);
      context.startService(pushIntent);
   }
}
s1ckb0y
  • 47
  • 4

1 Answers1

0

Yes for this you have to write broadcast receiver like this: Steps to launch new activity as follows:

1.Get intent for package

2.If intent is null redirect user to playstore

3.If intent is not null open activity

public class BroadCastDetecter extends BroadcastReceiver {
public static boolean internet_status = false;
public static void checkInternetConenction(Context context) {
    internet_status = false;
    ConnectivityManager check = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (check != null) {
        NetworkInfo[] info = check.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
            {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    internet_status = true;
                }
            }
        if(internet_status)
        {
           //do what you want to if internet connection is available
           launchNewActivity(context,"YOUR_PACKAGE_NAME");
        }
    }
}

@Override
public void onReceive(Context context, Intent intent)
{
    try {
        checkInternetConenction(context);
    }catch(Exception e){

    }
 }
}

public void launchNewActivity(Context context, String packageName) {
Intent intent = null;
if (android.os.Build.VERSION.SDK_INT >= 
 android.os.Build.VERSION_CODES.CUPCAKE) {
    intent = 
  context.getPackageManager().getLaunchIntentForPackage(packageName);
}
if (intent == null) {
    try {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.parse("market://details?id=" + packageName));
        context.startActivity(intent);
    } catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(Intent.ACTION_VIEW, 
  Uri.parse("https://play.google.com/store/apps/details?id=" + 
  packageName)));
    }
} else {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
 }
}

And In Manifest

<receiver android:name=".BroadCastDetecter">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
Ahmad Ayyaz
  • 774
  • 8
  • 25