2016-04-22 105 views
0

請解釋一下,關閉緊急撥號程序後,android如何調用鎖屏。我試圖檢查每一秒,如果緊急呼叫是最重要的活動,如果它不是,那麼我只是鎖定設備,但這並沒有做它的工作在Android 5.0和更高。所以,我需要另一種解決方案。如何知道手機應用程序是否關閉

我的代碼:

private static final long INTERVAL = TimeUnit.SECONDS.toMillis(1/100); 
    private Thread t = null; 
    private Context ctx = null; 
    private boolean running = false; 
public MyService2() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 

     running = true; 
     ctx = this; 
     Log.i("Point","PreRun"); 
     t = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       Log.i("Point","preloop"); 
       do { 
        Log.i("Point", "Prechech"); 
        if (activityManager.getRunningTasks(1).get(0).topActivity.getPackageName().equals("com.android.phone") || activityManager.getRunningTasks(1).get(0).topActivity.getPackageName().equals("com.android.dialer")) 
        { 
         try { 

          Thread.sleep(INTERVAL); 
         } catch (InterruptedException e) { 
          Log.i("Point", "Thread interrupted: 'KioskService'"); 
         } 
        } else { 
         running=false; 
         Log.i("Task is",activityManager.getRunningTasks(1).get(0).topActivity.getPackageName()); 
         stopSelf(); 
        } 
       }while (running) ; 
      } 
     }); 
     t.start(); 
     Log.i("Point","Done"); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i("Point","Service is stopped"); 
    } 
} 

而且這樣的:

public class MyActivity extends Activity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent("com.android.phone.EmergencyDialer.DIAL"); 
       startActivity(intent); 
       Intent intent1 = new Intent(MyActivity.this,MyService2.class); 
       startService(intent1); 
      } 
     }); 
    } 
} 
+0

請發佈您的完整代碼,以便我們可以幫助您。 – Lampard

+0

@crazyandroid我添加了我的代碼 –

+0

你在哪裏使用這個服務?@S.Drumble – Lampard

回答

0

可以啓動你的緊急撥號程序是這樣的:

Intent intent = new Intent("android.intent.action.MAIN"); 
intent.setComponent(ComponentName.unflattenFromString("com.android.phone/.EmergencyDialer")); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent); 

此外,請檢查您是否正確申報服務在清單中。

<service 
       android:name=".MyService2"> 
</service> 
+0

那麼,我的代碼是完全正常工作,我想要的是如果緊急撥號程序不在頂部,則顯示鎖定屏幕。 –

相關問題