2016-03-28 130 views
0

我需要殺死我的應用程序來刷新它以防止強制停止,因爲2個片段會從我們的服務器調用對象。我想要的是我的應用程序在片段視頻完成後刷新。我所做的是放在android中的killProcess後執行某些操作

android.os.Process.killProcess(android.os.Process.myPid()); 

fragmentTransaction.commit();。但它所做的是再次顯示視頻片段。同樣的事情發生時,我把方法調用片段更改後killProcess(我正在使用處理程序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); 
     } 
    }; 
} 
+0

如果你覺得你需要強制一個Android進程退出,你可能做錯了什麼。 –

+0

不,我不希望我的應用程序退出,我只是希望它在一段時間後刷新。 – rmanalo

+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;之前。它仍然是一樣的。感謝男人。

相關問題