gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:37:25 25 4
gpt4 key购买 nike

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

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

这很烦人。

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

upper 代码在接收器被存在错误的应用程序激活时复制到 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);
}

list :

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

最佳答案

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

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

关于android - 应用程序在收到广播时自动打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com