我创建的应用只能通过浏览器中的链接打开。但是,我需要从浏览器关闭该应用程序。我设法将参数传递给intent,以便我可以决定是关闭还是打开应用程序。我在代码中放了一些日志调试,这是我打开它时的logcat输出
07-10 15:06:52.383 20718-20718/xx.yy.,mypp D/MAIN﹕ ONCREATE
07-10 15:06:52.385 20718-20718/xx.yy.myapp D/PARAMS﹕ add
07-10 15:06:52.385 20718-20718/xx.yy.myapp D/PARAMS﹕ START
07-10 15:06:52.385 20718-20718/xx.yy.myapp D/MAIN﹕ READING PARAMS
07-10 15:06:52.386 20718-20718/xx.yy.myapp W/Resources﹕ Converting to string: TypedValue{t=0x1c/d=0x0 a=1 r=0x106000d}
07-10 15:06:52.388 20718-20718/xx.yy.myapp D/MAIN﹕ START
07-10 15:06:52.388 20718-20718/xx.yy.myapp D/MAIN﹕ startCameraService
07-10 15:06:52.388 20718-20718/xx.yy.myapp D/MAIN﹕ Floating camera OFF
07-10 15:06:52.388 20718-20718/xx.yy.myapp D/MAIN﹕ Starting service
07-10 15:06:52.394 20718-20718/xx.yy.myapp D/MAIN﹕ Floating camera ACTIVE
一切似乎都很好。然后我点击另一个链接关闭它,Logcat显示:
07-10 15:07:02.176 20718-20718/xx.yy.myapp D/MAIN﹕ ONCREATE
07-10 15:07:02.181 20718-20718/xx.yy.myapp D/PARAMS﹕ del
07-10 15:07:02.188 20718-20718/xx.yy.myapp D/PARAMS﹕ CLOSE
07-10 15:07:02.188 20718-20718/xx.yy.myapp D/MAIN﹕ READING PARAMS
07-10 15:07:02.194 20718-20718/xx.yy.myapp W/Resources﹕ Converting to string: TypedValue{t=0x1c/d=0x0 a=1 r=0x106000d}
07-10 15:07:02.195 20718-20718/xx.yy.myapp D/MAIN﹕ STOP
07-10 15:07:02.195 20718-20718/xx.yy.myapp D/MAIN﹕ stopCameraService
07-10 15:07:02.204 20718-20718/xx.yy.myapp D/MAIN﹕ Floating camera OFF
07-10 15:07:02.264 20718-20718/xx.yy.myapp D/MAIN﹕ DESTROY!!!
但该应用仍处于开放状态。这是MainActivity的代码:
package xx.yy.myapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends Activity {
private Intent cameraService = null;
private boolean actionStart = true;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences(getString(R.string.shared_prefs), MODE_PRIVATE);
editor = prefs.edit();
setContentView(R.layout.activity_main);
Log.d("MAIN", "ONCREATE");
}
@Override
protected void onResume()
{
super.onResume();
readParams();
Log.d("MAIN", "READING PARAMS");
if(actionStart) {
Toast.makeText(this, getText(R.string.camera_starting), Toast.LENGTH_LONG);
Log.d("MAIN", "START");
startCameraService();
moveTaskToBack(true);
onStateChange();
}
else
{
Toast.makeText(this, getText(R.string.camera_closing), Toast.LENGTH_SHORT);
Log.d("MAIN", "STOP");
stopCameraService();
}
finish();
}
@Override
protected void onDestroy()
{
Log.d("MAIN", "DESTROY!!!");
super.onDestroy();
}
private void readParams()
{
String cameraId = "";
String quality = "medium";
Uri data = getIntent().getData();
String host = data.getHost();
List<String> params = data.getPathSegments();
try {
cameraId = params.get(1);
Log.d("PARAMS", params.get(0));
actionStart = params.get(0).compareTo("add")==0;
quality = params.get(2);
}
catch(IndexOutOfBoundsException ibe)
{ /*sometimes quality param is not passed*/ }
Log.d("PARAMS", actionStart?"START":"CLOSE");
editor.putString("host", host);
editor.putString("id", cameraId);
editor.putString("quality", quality);
editor.apply();
}
private void startCameraService()
{
Log.d("MAIN", "startCameraService");
if(!checkService())
{
Log.d("MAIN", "Starting service");
this.cameraService = new Intent(this, FloatCamService.class);
startService(this.cameraService);
checkService();
}
}
private void stopCameraService()
{
Log.d("MAIN", "stopCameraService");
if(checkService()) {
Log.d("MAIN", "Stopping service");
stopService(this.cameraService);
}
}
private boolean checkService()
{
editor.putBoolean("serviceStatus", (this.cameraService != null && startService(this.cameraService) != null)).apply();
onStateChange();
return getServiceStatus();
}
public void toggleCamera(View clickedView)
{
if(!getServiceStatus()) {
this.startCameraService();
}
else
{
this.stopCameraService();
}
}
public void openStreaming(View clickedView)
{
startActivity(new Intent(this, StreamingActivity.class));
}
private boolean getServiceStatus()
{
return prefs.getBoolean("serviceStatus", false);
}
public void onStateChange() {
if (getServiceStatus()) {
Log.d("MAIN", getString(R.string.service_on));
} else {
Log.d("MAIN", getString(R.string.service_off));
}
}
}
Service onDestroy方法:
@Override
public void onDestroy() {
try {
Log.d("XXXXXXXXXXXXX", (container != null)? "NULL":"NOT NULL");
if (container != null) windowManager.removeView(container);
camHead.stop();
}catch(IllegalArgumentException iae)
{
}catch(NullPointerException npe)
{
}
super.onDestroy();
}