android - 从推送通知打开应用程序挂起应用程序

时间:2014-01-30 10:59:53

标签: android crash push-notification hang android-anr-dialog

最近几天,我在推送通知中注意到了奇怪的行为。 收到推送通知后,我可以点击它来打开应用程序。 它工作正常,直到随机,由于某种原因点击推送通知后应用程序想要打开,但完全挂起。结果是黑屏卡住和ANR。该应用程序需要被强制关闭。

我花了几个小时才找到原因。我发现了一件奇怪的事。 我从manifest.xml中删除了google Ads活动条目后,一切似乎都正常。当然除了广告。 现在我记得几个星期前我已经将谷歌游戏服务更新到了14版。

我的清单看起来像这样:

    <activity
        android:name="soccer.MainActivity"
        android:label="@string/Voetbal" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

来自广播接收器的onreceive我有以下代码

Intent intent = new Intent(context, MainActivity.class);

        intent.putExtra("matchid", matchid);


        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        } else{
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        intent.setData(Uri.parse("content://"+when));

        PendingIntent pendingIntent  = PendingIntent.getActivity(context, 0, intent, 0);

        NotificationManager notificationManager =(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                context.getApplicationContext())
                .setWhen(when)
                .setContentText(notificationContent)
                .setContentTitle(notificationTitle)
                .setSmallIcon(smalIcon)
                .setAutoCancel(true)
                .setTicker(notificationTitle)
                //.setLargeIcon(largeIcon)
                .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
                .setContentIntent(pendingIntent);

        Notification notification=notificationBuilder.build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

    try {
        notificationManager.notify(Integer.parseInt(matchid), notification);
        ;
    } catch (NumberFormatException e) {
        notificationManager.notify(0, notification);
    }

我绝对相信,当我删除谷歌广告活动时,问题就消失了。 我不知道发生什么事了。可能是因为Android正在尝试启动广告活动而不是MainActivity。当问题发生时,也不会调用“onCreate”。

我潜入了在ANR之后生成的traces.txt文件。它是一个巨大的文件,但它始于:

JNI: CheckJNI is off; workarounds are off; pins=0; globals=302 (plus 121 weak)

DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0)

"main" prio=5 tid=1 MONITOR
  | group="main" sCount=1 dsCount=0 obj=0x41b3cca8 self=0x41b2b3c8
  | sysTid=26971 nice=-6 sched=0/0 cgrp=apps handle=1074516308
  | state=S schedstat=( 0 0 0 ) utm=302 stm=78 core=0
  at aal.b(SourceFile:~287)
  - waiting to lock <0x42a2af28> (a java.lang.Object) held by tid=16 (AdWorker #1)
  at abm.f(SourceFile:33)
  at xq.r(SourceFile:593)
  at xq.b(SourceFile:168)
  at ya.onTransact(SourceFile:59)
  at android.os.Binder.transact(Binder.java:361)
  at com.google.android.gms.internal.ac$a$a.destroy((null):-1)
←[7m--more--←[0m

  at com.google.android.gms.ads.AdView.destroy((null):-1)
  at soccer.ContainerFragmentMatchInfo.onDestroy(ContainerFragmentMatchInfo.java:93)
  at android.support.v4.app.Fragment.performDestroy(Fragment.java:1720)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1056)
  at android.support.v4.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1201)
  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:639)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
  at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:456)
  at com.actionbarsherlock.app.SherlockFragmentActivity.onPostResume(SherlockFragmentActivity.java:68)
  at android.app.Activity.performResume(Activity.java:5323)
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
  at android.app.ActivityThread.access$800(ActivityThread.java:135)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  at android.os.Handler.dispatchMessage(Handler.java:102)

UPDATE ::

如果我从我的应用程序中删除以下代码,一切似乎再次起作用:

     @Override
  public void onPause() {
    adView.pause();
    super.onPause();
  }

  @Override
  public void onResume() {
    super.onResume();
    adView.resume();
  }

  @Override
  public void onDestroy() {
    adView.destroy();
    super.onDestroy();
  }

我不知道为什么。我不知道影响是什么。

1 个答案:

答案 0 :(得分:1)

正如您在Android文档中看到的那样(例如here),您应该始终首先为所有生命周期方法调用超类方法。

在超类方法之前调用adView方法可能是导致问题的原因。

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first

    // Release the Camera because we don't need it when paused
    // and other activities might need to use it.
    if (mCamera != null) {
        mCamera.release()
        mCamera = null;
    }
}

因此,您可以将其更改为:

,而不是删除该代码
  @Override
  public void onPause() {
    super.onPause();
    adView.pause();
  }

  @Override
  public void onResume() {
    super.onResume();
    adView.resume();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    adView.destroy();
  }