0

每当我按下应用程序上的主页按钮然后再次返回时,应用程序似乎已停止。我真的不知道为什么,因为我还没有做任何事情。所以这意味着唯一被执行的是我的 oncreate、onresume、onpause 和 on destroy。我没有在 onresume、onpause 和 destroy 中做任何事情。还没有服务启动,或者只是在 oncreate 中加载和初始化数据。另一件事是,它没有崩溃,它只是停止或关闭。没有错误。我将与您分享我的 onCreate 按钮。也许您会看到导致这种情况发生的原因。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.layout_landstar_page);

        preferences = getSharedPreferences(pref_data, Context.MODE_PRIVATE);
        alertDialogBuilder = new AlertDialog.Builder(this);
        i = new Intent(this, BGService.class);

        shipmentAvailableLayout = ((LinearLayout)findViewById(R.id.shipment_available_layout));
        shipmentNotAvailableLayout = ((LinearLayout)findViewById(R.id.shipment_notavailable_layout));
        menu = ((RelativeLayout)findViewById(R.id.menu));
        declineBtn = ((Button)findViewById(R.id.declinebtn));
        acceptBtn = ((Button)findViewById(R.id.acceptbtn));
        callHelpDeskBtn = ((Button)findViewById(R.id.callhelpdeskbtn));
        menuBtn = ((ImageView)findViewById(R.id.menubtn));
        refreshBtn = ((ImageView)findViewById(R.id.refreshbtn));
        logoutBtn = ((ImageView)findViewById(R.id.logoutbtn));
        trackingMsgShipment = ((TextView)findViewById(R.id.tracking_message_shipment));

        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyWakeLock");

        registerReceiver(broadcastReceiver, new IntentFilter(BGService.BROADCAST_ACTION));

        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        ((Button)findViewById(R.id.acceptbtn)).setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                    acceptBtn.setBackgroundResource(R.drawable.accept_selected);
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    acceptBtn.setBackgroundResource(R.drawable.accept_idle);

                    if(isNetworkConnected())
                    {
                        acceptLoad();

                    }
                    else
                        noConnection();
                }
                return false;
            }
        });

        ((Button)findViewById(R.id.declinebtn)).setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                    declineBtn.setBackgroundResource(R.drawable.decline_selected);
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    declineBtn.setBackgroundResource(R.drawable.decline_idle);
                    declineLoad();
                }
                return false;
            }
        });

        ((Button)findViewById(R.id.callhelpdeskbtn)).setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                    callHelpDeskBtn.setBackgroundResource(R.drawable.btn_refresh_selected);
                if(event.getAction()==MotionEvent.ACTION_UP)
                {
                    callHelpDeskBtn.setBackgroundResource(R.drawable.btn_refresh_idle);
                    if(isNetworkConnected())
                    {
                        displayProgressSpinner();
                    }
                    else
                        noConnection();
                }
                return false;
            }
        });

        ((ImageView)findViewById(R.id.menubtn)).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    if(menu.isShown())
                    {
                        menu.setVisibility(View.GONE);
                    }
                    else
                    {
                        menu.setVisibility(View.VISIBLE);
                    }
                }
                return false;
            }
        });

        ((ImageView)findViewById(R.id.refreshbtn)).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    menu.setVisibility(View.GONE);
                    if(isNetworkConnected())
                        displayProgressSpinner();
                    else
                        noConnection();
                }
                return false;
            }
        });

        ((ImageView)findViewById(R.id.logoutbtn)).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    menu.setVisibility(View.GONE);

                    try{
                        checkerTimer.cancel();
                        gpsTimer.cancel();
                        counterTimer2.cancel();
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.i("isAccept", preferences.getString("isAccepted",""));

                    alertDialogBuilder.setTitle("Message");
                    alertDialogBuilder
                    .setMessage("Clicking the log out button will stop your GPS and exit the application. Are you sure?")
                        .setCancelable(false)
                        .setNegativeButton("Logout",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                                SharedPreferences.Editor editor = preferences.edit();
                                editor.putString("isDriverLogin", "True");
                                editor.putString("driverPassword", driverPassword);
                                editor.putString("carrierId", carrierId);
                                editor.putString("CCTID", cctid);
                                editor.putString("shipment", entityShipment);
                                editor.putString("isAccepted", "");
                                editor.commit();
                                getApplicationContext().stopService(i);
                                finish();
                            }
                         })
                        .setPositiveButton("Cancel",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                            }
                          });


                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();

                }
                return false;
            }
        });


        Intent intent = getIntent();
        loadSaveDetails(intent);
    }

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    >
    <activity
        android:name="com.nesv.landstar.SplashScreen"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name="com.nesv.landstar.Login"
        android:label="@string/app_name" 
         android:screenOrientation="portrait">
    </activity>

    <activity
        android:name="com.nesv.landstar.DriverLogin"
        android:label="@string/app_name" 
         android:screenOrientation="portrait">
    </activity>

    <activity
        android:name="com.nesv.landstar.LandstarPage"
        android:label="@string/app_name" 
         android:screenOrientation="portrait">
    </activity>

    <service android:name="com.nesv.landstar.BGService"></service>

</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.WAKE_LOCK" />

有一件事,这不会发生在有根的三星 s3 和普通的三星 s4 上。在 S duos 4.0 版和更低的姜饼上尝试过。这发生了。

另一个问题。我尝试从 Eclipse 安装它。这没有发生。仅当我尝试安装来自设备上的 bin 文件夹的 apk 文件时才会发生这种情况。

4

2 回答 2

0

试试下面的代码。

public class StartupActivity extends Activity {
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            if (needStartApp()) {
                Intent i = new Intent(StartupActivity.this, MainActivity.class);
                startActivity(i);
            }

            finish();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            // this prevents StartupActivity recreation on Configuration changes 
            // (device orientation changes or hardware keyboard open/close).
            // just do nothing on these changes:
            super.onConfigurationChanged(null);
        }

        private boolean needStartApp() {
            final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);

            if (!tasksInfo.isEmpty()) {
                final String ourAppPackageName = getPackageName();
                RunningTaskInfo taskInfo;
                final int size = tasksInfo.size();
                for (int i = 0; i < size; i++) {
                    taskInfo = tasksInfo.get(i);
                    if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
                        // continue application start only if there is the only Activity in the task
                        // (BTW in this case this is the StartupActivity)
                        return taskInfo.numActivities == 1;
                    }
                }
            } 

            return true;
        }

}

从此启动活动启动您的家庭活动。在清单中

<activity android:name=".StartupActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
于 2013-09-25T04:56:27.117 回答
0

覆盖 onPause()、onStop() 和 onDestroy()(确保在它们中调用 super.onXXX())并在每个中放置一个日志语句,以查看系统是否决定销毁您的活动,例如因为系统内存低。如果是这样,那么当您返回 Activity 时,将需要再次调用 onCreate()。

“开发工具”应用程序有一个名为“立即销毁活动”的设置,无论系统是否需要 RAM,当您离开活动时,它总是会销毁活动。

于 2013-09-25T02:17:24.410 回答