如何知道手机应用是否已关闭

时间:2016-04-22 01:48:46

标签: android

请解释我关闭紧急拨号器后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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分: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>