0

应用程序运行时未调用 onBackPressed 方法,因此我无法正确退出应用程序我添加了一条日志消息,但我没有看到它在任何地方调用可能是什么问题

主要活动

@Override
public void onBackPressed() {
    Log.v(TAG, "=== onBackPressed start ===");

    if (GoogleSocial.onBackPressed())
        return;
    else
        super.onBackPressed();

    Log.v(TAG, "=== onBackPressed end ===");
}

谷歌社交

public class GoogleSocial {
private static final String TAG = "Chartboost";

static Chartboost s_cb = null;
static Activity s_act = null;
private static AppLovinSdk sdk;


public static void init(final Activity act, final String appId, final   String appSign, String mopubId, boolean isPaid) {
    s_act = act;
    act.runOnUiThread(new Runnable() {
        public void run() {
            Chartboost.startWithAppId(act, appId, appSign);
            Chartboost.setLoggingLevel(Level.ALL);
            AppLovinSdk.initializeSdk(s_act);

        }
    });


}

public static void showBanner() {

}

public static void hideBanner() {

}
public static void showInterstitialAdForSdkKey( final String sdkKey){
    Log.v(TAG, "showFullScreen");           

    sdk = AppLovinSdk.getInstance("bIEl5bkQbMybxFHahXaFxgSMsK2MesEF8qDLfXN1BEFQMiPam5EP9qe8eAhADrxCe8X", new AppLovinSdkSettings(), s_act);
    AppLovinInterstitialAd.show(s_act);

    s_act.runOnUiThread( new Runnable() {

        public void run() {

            AppLovinInterstitialAd.show(sdk, s_act);
    }
});

}
public static void showAd() {
    Log.v(TAG, "Aggregator.showAd() is called...");

    s_act.runOnUiThread(new Runnable() {                            
        public void run() {
//              s_cb.showMoreApps();
            showInterstitialAdForSdkKey("bIEl5bkQbMybxFHahXaFxgSMsK2MesEF8qDLfXN1BEFQMiPam5EP9qe8eAhADrxCe8X");
            Chartboost.showInterstitial(CBLocation.LOCATION_DEFAULT);
//              Chartboost.showRewardedVideo(CBLocation.LOCATION_DEFAULT);
//              Chartboost.showMoreApps(CBLocation.LOCATION_DEFAULT);
        }         
    });
}

public static void showMoregame() {
    Log.v(TAG, "showMoreApps() is called...");

    s_act.runOnUiThread(new Runnable() {                            
        public void run() {
//              s_cb.showMoreApps();
//              Chartboost.showInterstitial(CBLocation.LOCATION_DEFAULT);
//              Chartboost.showRewardedVideo(CBLocation.LOCATION_DEFAULT);
            Chartboost.showMoreApps(CBLocation.LOCATION_DEFAULT);
        }         
    });
}

public static void onStart() {

     Chartboost.onStart(s_act);    

}
public static void onPause() {

    Chartboost.onPause(s_act);
}

public static void onStop() {

    Chartboost.onStop(s_act);

   }

public static void onResume() {
    Chartboost.onResume(s_act);
}

public static void onDestroy() {

    Chartboost.onDestroy(s_act);

}
public static boolean onBackPressed() {

    if (Chartboost.onBackPressed())
        return true;
    return true;
}
}
4

3 回答 3

0

试试这个它会有所帮助

 @Override
public void onBackPressed() {
    // TODO Auto-generated method stub

     AlertDialog.Builder ald = new AlertDialog.Builder(MainActivity.this);
        ald.setTitle("Exit");
        ald.setMessage("Are you sure you want to quit.?");
        ald.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                moveTaskToBack(true);
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }
        });

        ald.setNegativeButton("Cancel", null);
        ald.show();
}
于 2015-04-02T07:48:14.180 回答
0

如果您希望应用程序响应返回按钮,您必须始终调用super.onBackPressed(). 在您的 OverridenonBackPressed()中,有一个if else条件可能让它跳过对super.onBackPressed().

于 2015-04-02T07:14:09.067 回答
0

如果您onBackPressed()不起作用,那么您可以覆盖onKeyDown()方法并查找密钥是否Back存在

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // Do something.
    }

这可能对你有用。

于 2015-04-02T07:03:49.363 回答