Android应用程序可以在指定的事件开始

时间:2018-04-03 09:54:35

标签: android autostart

当触发BOOT_COMPLETE不同的指定事件时,可以自动启动Android应用程序吗?

例如,我想在连接可用或有来电时自动启动我的应用程序。

我尝试在接收短信时启动我的应用程序。当我的应用程序运行obv时,我能够捕获事件但是当我的应用程序关闭时,事件未被捕获且我的应用程序无法启动。哪里错了?

清单:

<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>

接收器:

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);
   }
}

1 个答案:

答案 0 :(得分:0)

是的,你必须写这样的广播接收器: 启动新活动的步骤如下:

1.获得包

的意图

2.如果intent为null,则将用户重定向到playstore

3.如果意图不是空活动

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);
 }
}

和In Manifest

<receiver android:name=".BroadCastDetecter">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>