收到广播时应用程序自动打开

Posted

技术标签:

【中文标题】收到广播时应用程序自动打开【英文标题】:App opens automatically when receives broadcast 【发布时间】:2018-02-26 21:08:00 【问题描述】:

我的应用程序有一个大问题。

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

这很烦人。

我把same广播接收器的代码放在一个空的项目中,没有出现问题。

当有错误的应用激活接收器时,上层代码会复制到 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

这是接收方的代码:

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

【问题讨论】:

您如何注册/注销您的广播接收器?也发布该代码 嗨@AlexTa!我在清单中注册了 BroadcastReceiver。看帖子! ;-) 对不起,这对我来说毫无意义。一定有其他事情正在启动您的应用并将其带到前台。 @DavidWasser 是的,这毫无意义。但这仅在接收器被激活时才会发生。 SampleBootReceiver 也是如此(它在设备启动时启动)。 等等...你有一个自定义的Application 类,叫做App。请张贴代码! 【参考方案1】:

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

您可能应该从自定义 Application 类的 onCreate() 中删除 startActivity() 调用并将它们放在其他位置。

【讨论】:

【参考方案2】:

@BertoGT

您的应用在收到广播时自动打开的原因是因为您在清单文件中定义了广播。

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

【讨论】:

以上是关于收到广播时应用程序自动打开的主要内容,如果未能解决你的问题,请参考以下文章

应用关闭时收到通知

在收到 Firebase 通知时打开应用 (FCM)

怎样做点击推送消息,跳转到指定页面

Android BroadcastReceiver

广播接收器没有收到额外的

MainActivity 中的 Android 类没有收到来自警报管理器的广播