在android中的killProcess之后做一些事情

时间:2016-03-28 10:02:25

标签: android android-fragments

我需要杀死我的应用程序以刷新它以防止强制停止,因为2个片段从我们的服务器调用对象。我想要的是我的应用程序在片段上的视频完成后刷新。我做了什么是

android.os.Process.killProcess(android.os.Process.myPid());
fragmentTransaction.commit();之后

。但它的作用是再次显示视频片段。当我在调用片段更改的方法调用之后放入killProcess(我正在使用handler-runnable来更改我的片段)时,会发生同样的事情。所以,killProcess也会杀死变量。我无法找到关于这些东西的任何信息。

这是我的代码:

public class fragChangeView extends AppCompatActivity {
    FrameLayout frmLayout;
    FragmentTransaction ft;
    FragmentManager fm;
    int fragNum;
    Handler handler;
    private LuksloftsFunc lFunc;
    int changeFragDelay, changeFragDelay2;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    int durationFromVidLen;
    Long longChangeDelay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frag_change_view);

        frmLayout = (FrameLayout)findViewById(R.id.frmFragLayout);
        Intent i = getIntent();
        String actName = i.getStringExtra("actName");

        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        handler = new Handler();

        retriever.setDataSource("http://*video ip*/api/extras/permit_video.mp4", new HashMap<String, String>());
        String vidLen = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        durationFromVidLen = Integer.parseInt(vidLen);

        lFunc = LuksloftsFunc.getInstance(this);
        changeFragDelay = Integer.parseInt(lFunc.getTimezoneCurrencyInterval());
        longChangeDelay = Long.parseLong(lFunc.getTimezoneCurrencyInterval());
        lFunc.updateAppLastRefreshTime();

        //startAlarmFragChange();

        switch (actName){
            case "video":
                PermitVideoFrag frag1 = new PermitVideoFrag();
                ft.replace(R.id.frmFragLayout, frag1);
                ft.commit();
                fragNum = 1;
                break;
            case "timezone":
                ForexTimezoneFrag frag2 = new ForexTimezoneFrag();
                ft.replace(R.id.frmFragLayout, frag2);
                ft.commit();
                fragNum = 2;
                break;
        }
        try{
            handler.post(changeFrag);
        }catch(Exception e){

        }

    }

    public void startAlarmFragChange(){
        //start alarmmanager fragChanger
        AlarmManager am =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplication(), AlarmFragChange.class);
        PendingIntent pi = PendingIntent.getBroadcast(getApplication(), 0, intent, 0);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), longChangeDelay, pi);
        //end alarmmanager fragChanger
    }

    public void stopAlarmFragChange(){
        Intent intent = new Intent(getApplication(), AlarmFragChange.class);
        PendingIntent sender = PendingIntent.getBroadcast(getApplication(), 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getApplication().getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }

    public void fragChanger(){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

            switch (fragNum) {
                case 1:
                    ForexTimezoneFrag frag2 = new ForexTimezoneFrag();
                    ft.replace(R.id.frmFragLayout, frag2);
                    fragNum = 2;
                    changeFragDelay2 = changeFragDelay;
                    break;
                case 2:
                    PermitVideoFrag frag1 = new PermitVideoFrag();
                    ft.replace(R.id.frmFragLayout, frag1);
                    fragNum = 1;
                    changeFragDelay2 = durationFromVidLen;
                    break;
            }
            try {
                ft.commit();
                Log.d("log1", "error on commit");
                /*if(lFunc.IsNeedAppRefresh()) {
                   android.os.Process.killProcess(android.os.Process.myPid());
                }*/
            } catch (Exception e) {

            }
    }

    private Runnable changeFrag = new Runnable() {
        @Override
        public void run() {
            fragChanger();
            /*if(lFunc.IsNeedAppRefresh()) {

                Log.d("", "after kill process");
                android.os.Process.killProcess(android.os.Process.myPid());
            }*/
            //handler.postDelayed(changeFrag, changeFragDelay);
            handler.postDelayed(changeFrag, changeFragDelay2);
        }
    };
}

1 个答案:

答案 0 :(得分:0)

我终于想出了该怎么做。我像这样更新了代码;

public void fragChanger(){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        String fragName = "";
            switch (fragNum) {
                case 1:
                    ForexTimezoneFrag frag2 = new ForexTimezoneFrag();
                    ft.replace(R.id.frmFragLayout, frag2);
                    fragNum = 2;
                    fragName = "forex";
                    changeFragDelay2 = changeFragDelay;
                    break;
                case 2:
                    PermitVideoFrag frag1 = new PermitVideoFrag();
                    ft.replace(R.id.frmFragLayout, frag1);
                    fragNum = 1;
                    fragName = "video";
                    changeFragDelay2 = durationFromVidLen;
                    break;
            }
            try {
                ft.commit();
                Log.d("log1", "fragName: "+fragName);
                if(lFunc.IsNeedAppRefresh()) {
                    Log.d("log1", "app will refresh");
                    android.os.Process.killProcess(android.os.Process.myPid());
                }

            } catch (Exception e) {
                Log.d("log1", "error on commit");

            }
    }

我也可以把它放在break;之前。它仍然有效。谢谢你们。