一个应用程序接收广播而另一个不接收。为什么?

时间:2014-07-22 07:22:04

标签: android android-intent android-alarms android-broadcast

我有3个相互依赖的应用程序。例如,客户端应用程序,浏览器和启动器。我的要求是卸载客户端时(仅当客户端)我还要卸载其他两个应用程序。为此我到目前为止所做的是,我已经编写了一个广播接收器(在两个应用程序中),用于侦听已删除的包。如果删除的包是我的客户端,我会启动一个启动卸载程序的警报(在两个应用程序上单独启动)。为什么报警?因为我希望卸载弹出窗口直到用户卸载应用程序。我目前面临的问题是,有时候发射器应用程序没有得到广播,所以我认为。因此它不会启动卸载。有时确实如此,有时却没有。我无法弄清楚这个问题的原因。下面是启动器中写的代码:

广播接收器

public class UnistallBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            String packagename = intent.getData().getSchemeSpecificPart();
            if (packagename.equalsIgnoreCase("com.android.nanoheal")) {
                Calendar calendar = new GregorianCalendar();
                calendar.setTimeZone(TimeZone.getDefault());
                calendar.setTimeInMillis(System.currentTimeMillis());

                calendar.set(Calendar.MINUTE, 00);
                calendar.set(Calendar.SECOND, 00);

                Intent intent1 = new Intent(context, AlarmIntent.class);
                intent1.putExtra("appnamel", "com.nanoheal.launcher");

                PendingIntent pi = PendingIntent.getBroadcast(context, 325,
                        intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) context
                        .getSystemService(Context.ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(), 1000 * 30, pi);
            }

        }

    }
}

警报意图:

public class AlarmIntent extends BroadcastReceiver {
    Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("AlarmIntent", "Intent fired by Launcher");
        mContext = context;
        try {
            String appLauncher = intent.getStringExtra("appnamel");
            if (appLauncher != null) {
                if (isAppInstalled(appLauncher)) {

                    Uri packageURI2 = Uri.parse("package:" + appLauncher);
                    Intent uninstallIntent2 = new Intent(
                            Intent.ACTION_UNINSTALL_PACKAGE, packageURI2);
                    uninstallIntent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(uninstallIntent2);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isAppInstalled(String packageName) {
        PackageManager pm = mContext.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        Log.d("AlarmIntent", "Launcher-IsAppInstalled " + packageName + " "
                + installed);
        return installed;
    }

}

1 个答案:

答案 0 :(得分:0)

我不确定如何以及为何的详细信息。  但我改变了这个

<receiver android:name="package.UnistallBroadcast" >
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

<receiver android:name="package.UnistallBroadcast" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

它工作正常。据我所知,如果为接收者设置了优先级,那么具有最高优先级的优先级应该首先获得该动作。但奇怪的是,在我的情况下,我没有得到接收器的动作我优先考虑。