接收广播时,应用会自动打开

时间:2017-09-18 10:48:18

标签: android android-activity broadcastreceiver android-notifications

我的应用程序存在很大问题。

当它关闭(不在后台)并且收到广播时,应用打开并置于前景

非常讨厌。

我将相同广播接收器的代码放在一个空项目中,问题不会出现。

当具有错误的应用激活接收器时,代码将复制到AndroidMonitor中。

更低,应用程序没有问题。



09-18 12:28:34.893 26455-26455/? I/art: Late-enabling -Xcheck:jni
09-18 12:28:34.960 26455-26455/agm.fisioapp W/System: ClassLoader referenced unknown path: /data/app/agm.fisioapp-2/lib/arm
09-18 12:28:35.063 26455-26455/agm.fisioapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-18 12:28:35.141 26455-26455/agm.fisioapp D/AutoManageHelper: starting AutoManage for client 0 false false
09-18 12:28:35.160 26455-26455/agm.fisioapp D/AutoManageHelper: onStart true {0=com.google.android.gms.internal.zzaaa$zza@945134}
09-18 12:28:35.206 26455-26499/agm.fisioapp I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3_RB1__release_AU (Ia6c73e7530)
                                                          OpenGL ES Shader Compiler Version: E031.29.00.00
                                                          Build Date: 12/04/15 Fri
                                                          Local Branch: mybranch17080070
                                                          Remote Branch: quic/LA.BF.1.1.3_rb1.5
                                                          Local Patches: NONE
                                                          Reconstruct Branch: NOTHING
09-18 12:28:35.215 26455-26499/agm.fisioapp I/OpenGLRenderer: Initialized EGL, version 1.4
09-18 12:28:35.215 26455-26499/agm.fisioapp D/OpenGLRenderer: Swap behavior 1
09-18 12:28:35.217 26455-26499/agm.fisioapp W/Adreno-ES20: <get_gpu_clk:229>: open failed: errno 13
09-18 12:28:35.250 26455-26491/agm.fisioapp W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
09-18 12:28:35.252 26455-26491/agm.fisioapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default

---------------------------------------------------------------------------------------

09-18 12:30:35.063 28050-28050/? I/art: Late-enabling -Xcheck:jni
09-18 12:30:35.086 28050-28057/? I/art: Debugger is no longer active
09-18 12:30:35.086 28050-28057/? I/art: Starting a blocking GC Instrumentation
09-18 12:30:35.104 28050-28050/? W/System: ClassLoader referenced unknown path: /data/app/com.javatechig.alarmservice-2/lib/arm
09-18 12:30:35.113 28050-28050/? D/prova: prova
&#13;
&#13;
&#13;

这是接收者的代码:

public class CreatoreNotificaAllenamento extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent in = new Intent(context, MainActivity.class);
    long[] pattern = {0, 500, 1};
    PendingIntent pi = PendingIntent.getActivity(context, 01234, in, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.icona_notifica)
            .setContentTitle(context.getString(R.string.notifica_titolo_ricordo_allenamnto))
            .setContentText(context.getString(R.string.testo_notifica))
            .setVibrate(pattern)
            .setAutoCancel(true);

    mBuilder.setContentIntent(pi);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}

这是我用来启动AlarmService的方法:

public static void setAlarm(Context c, Date data) {
    AlarmManager alarmMgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(c, CreatoreNotificaAllenamento.class);
    PendingIntent pi = PendingIntent.getBroadcast(c, 0, intent, 0);

    if(System.currentTimeMillis() > data.getTime()) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(data);
        cal.add(Calendar.DAY_OF_YEAR, 1);
        data = cal.getTime();
    }
    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, data.getTime(),
            1000*60*2, pi);
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="agm.fisioapp">
/*
PERMESSI
*/
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:name=".App"
    android:allowBackup="true"
    android:icon="@mipmap/icona"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name=".Algoritmi.CreatoreNotificaAllenamento" />
    <receiver android:name=".Algoritmi.SampleBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    /*
     other activities
    */

</application>

App.java代码:

public class App extends Application {

@Override
public void onCreate() {
    super.onCreate();

    SharedPreferences primoAvvio = getSharedPreferences(Costanti.preferenzeMiste, MODE_PRIVATE);
    if(!primoAvvio.contains("primoAvvio")) {
        // se entro qua vuol dire che è il primo avvio dell'app
        try {
            caricaDatiSulDB();
            SharedPreferences.Editor editor = primoAvvio.edit();
            editor.putBoolean("primoAvvio", false);
            editor.apply();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else try{
        MainModel model = new MainModel(getApplicationContext());
        if(controlloVersione()) {
            model.cancellaDati();
            caricaDatiSulDB();
        }
    } catch (IOException ex){
        ex.printStackTrace();
    }

    // Controlla se l'utente è loggato.
    SharedPreferences prefs = getSharedPreferences(Costanti.preferenzeDatiUtente, MODE_PRIVATE);
    if(!prefs.contains("idUtente") && prefs.getBoolean("isGoogle",false) == false){
        startActivity(new Intent(getApplicationContext(), LoginActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    } else {
        // se esistono dei dati di login salvati eseguilo in background.
        startActivity(new Intent(getApplicationContext(), BackgroundLogin.class)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}

/**
 * Controlla se va fatto l'aggiornamento degli es/patologie/ecc..
 * @return true se va aggiornato, false altrimenti
 */
private boolean controlloVersione() throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(getAssets().open("version.txt")));
    float version = Float.parseFloat(in.readLine());
    SharedPreferences preferences = getSharedPreferences(Costanti.preferenzeMiste, MODE_PRIVATE);
    if(version != preferences.getFloat("versione", 0)){
        SharedPreferences.Editor editor = preferences.edit();
        editor.putFloat("versione", version);
        editor.apply();
        return true;
    } else
        return false;
}

private void caricaDatiSulDB() throws IOException {
    MainModel model = new MainModel(getApplicationContext());
    // carico i dati nel db locale
    model.eserciziToDB(model.lettura(FILE_ESERCIZI));
    model.patologieToDB(model.lettura(FILE_PATOLOGIE));
    model.protocolliToDB(model.lettura(FILE_PROTOCOLLI));
    model.faseToDB(model.lettura(FILE_FASI));
    model.defaultToDB(model.lettura(FILE_ESERCIZIO_FASI));
    model.consigliToDB(model.lettura(FILE_CONSIGLIO));
    model.specificheToDB(model.lettura(FILE_SPECIFICHE));
    model.curiositaToDB(model.lettura(FILE_CURIOSITA));
}

}

2 个答案:

答案 0 :(得分:0)

我怀疑。当您的应用未运行且触发了Application时(例如,在BOOT时间),Android会为您的应用创建新的操作系统流程,并创建自定义onCreate()类的新实例,并且在该实例上调用startActivity()。在您的代码中,拨打自定义onCreate()课程Application内的Activity。当然,这会将您的应用程序带到前台并显示已启动的startActivity()。你可能不希望这种情况发生。

您可能应该从自定义onCreate()课程的Application中移除#import <CommonCrypto/CommonCryptor.h>来电并将其放在其他位置。

答案 1 :(得分:0)

@BertoGT

由于您已在清单文件中定义广播,因此您的应用在接收广播时会自动打开。

要解决您的问题,您必须在您的班级注册广播接收器。然后在onResume方法中注册广播接收器并在onPause或onDestroy方法上取消注册广播接收器。您的问题将得到解决。