Android,是否可以在后台运行应用程序?

时间:2015-07-21 14:01:55

标签: android

我正在设计一个具有以下要求的应用程序:

  1. 添加多个地址并在地图上显示地址路径。
  2. 当用户点击开始路线按钮时,googles“turn-by-turn”应用程序将被打开,我的应用程序将进入后台。
  3. 当用户到达特定地址时,我的应用程序再次进入前台并显示一条弹出消息,然后在谷歌“转弯应用程序”中启动另一条路径,我使用广播接收器将我的应用程序带到前台
  4. 我的问题是,当我的应用程序进入后台并且“转弯”应用程序启动时,用户到达特定地址后,有时它将无法返回到我的应用程序,当我故意打开我的应用程序时,它将不会在运行模式下。

    编辑问题:

    我已经创建了启动近距离警报的服务,以通知该位置:

    public class LocationUpdateBoundService extends Service implements LocationListener{
    private static final long POINT_RADIUS = 200; // in Meters
    private static final long PROX_ALERT_EXPIRATION = -1;
    private static final String PROX_ALERT_INTENT = "com.routerapp.ProximityAlert";
    private final BroadcastReceiver tProximityAlrt = new ProximityIntentReceiver();
    PendingIntent proximityIntent;
    
    private static int requestCode = 1;
    private LocationManager locationManager; 
    public Location location;
    @Override
    public IBinder onBind(Intent intent) {
    
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try{
    
            int LAST_COUNT= intent.getIntExtra("LAST_COUNT",90);
             double LATITUDE=intent.getDoubleExtra("LATITUDE",0);
             double LONGITUDE=intent.getDoubleExtra("LONGITUDE",0);
             String ROUTE_NAME=intent.getStringExtra("ROUTE_NAME");
             String ROUTE_ADDRESS=intent.getStringExtra("ROUTE_ADDRESS");
             String ROUTE_NOTE=intent.getStringExtra("ROUTE_NOTE");
             int ROUTE_ID=intent.getIntExtra("ROUTE_ID",90);
             int ROUTE_TOTAL=intent.getIntExtra("ROUTE_TOTAL",90);
             boolean STOP_WATCH=intent.getBooleanExtra("STOP_WATCH",false);
    
         Toast.makeText(this, "Service ROUTE_NAME="+LATITUDE, Toast.LENGTH_SHORT).show();
         locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
         Criteria criteria = new Criteria();
            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
    
        // Getting Current Location From GPS
                    location = locationManager.getLastKnownLocation(provider);
                    // Log.d(TAG, "route_size 1= " + location.getLatitude());
                    // onLocationChanged(location);
                    if (location != null) {
                        //Log.d(TAG, "route_size2= " + route_size);
                        onLocationChanged(location);
    
                    }
         locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, this);
        //Toast.makeText(this, "SERVICE START", Toast.LENGTH_SHORT).show();
         addProximityAlert(LATITUDE,LONGITUDE,ROUTE_ADDRESS,ROUTE_NOTE,ROUTE_ID,STOP_WATCH,ROUTE_NAME,ROUTE_TOTAL);
        }catch(NullPointerException e)
        {
    
        }
    
    
    
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    
    }
    
    private void addProximityAlert(double latitude, double longitude,
             String ROUTE_ADDRESS, String ROUTE_NOTE, int ROUTE_ID, boolean stop_watch_activated,String route_name,int total_route) {
    
        Bundle extras = new Bundle();
        extras.putString("location_title", route_name);
        extras.putString("location", ROUTE_ADDRESS);
        extras.putString("note", ROUTE_NOTE);
        extras.putInt("unique_id", ROUTE_ID);
        extras.putInt("total_route", total_route);
        extras.putBoolean("stop_watch", stop_watch_activated);
    
        Intent intent = new Intent(PROX_ALERT_INTENT);
        intent.putExtra(PROX_ALERT_INTENT, extras);
         proximityIntent = PendingIntent.getBroadcast(
                LocationUpdateBoundService.this, requestCode, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        locationManager.addProximityAlert(latitude, // the latitude of the
                                                    // central point of the
                                                    // alert region
                longitude, // the longitude of the central point of the alert
                            // region
                POINT_RADIUS, // the radius of the central point of the alert
                                // region, in meters
                PROX_ALERT_EXPIRATION, // time for this proximity alert, in
                                        // milliseconds, or -1 to indicate no
                                        // expiration
                proximityIntent // will be used to generate an Intent to fire
                                // when entry to or exit from the alert region
                                // is detected
                );
        requestCode++;
    
        initialiseAlertReceiver() ;
    
    }
    
    
    private void initialiseAlertReceiver() {
        // PROXI ALERT FOR NOTE
        IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
        registerReceiver(tProximityAlrt, filter);
    
    }
    
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void onDestroy() {
    Toast.makeText(LocationUpdateBoundService.this, "SERVICE DISTROYED", Toast.LENGTH_SHORT).show();
    // DEACTIVATE PROXIMITYALER
    locationManager.removeProximityAlert(proximityIntent);
    unregisterReceiver(tProximityAlrt);
        super.onDestroy();
    }
    

    }

    public class ProximityIntentReceiver extends BroadcastReceiver {
    String location_title;
    String location;
    String note;
    int total_route;
    private static int unique_id = 0;
    
    boolean is_alertAvailable=false;
    boolean stop_watch;
    //private SettingSharedPreference pref;
    @Override
    public void onReceive(Context context, Intent intent) {
        is_alertAvailable=false;
    
    
        Bundle bndlBundle=intent.getBundleExtra("com.routerapp.ProximityAlert");
    
        unique_id = bndlBundle.getInt("unique_id");
    
    
        total_route= bndlBundle.getInt("total_route");
        location = bndlBundle.getString("location");
        location_title = bndlBundle.getString("location_title");
        note = bndlBundle.getString(
                "note");
        stop_watch=bndlBundle.getBoolean("stop_watch");
    
        Toast.makeText(context, "BroadcastReceiver START="+location, Toast.LENGTH_SHORT).show();
        if(!stop_watch)
        {
            //Toast.makeText(context, "LAST POINT REACHED", Toast.LENGTH_SHORT).show();
            DrivingRouteActivity.is_last_point=true;
        }
    
    
        for(Integer uniqueID:DrivingRouteActivity.tArrayOfBroaCastReceiver)
        {
            if(uniqueID==unique_id)
            {
                is_alertAvailable=true;
                break;
            }
        }
        //OPEN THE APPLICATION MAP ACTIVITY
        if(!is_alertAvailable)
        {
            DrivingRouteActivity.tArrayOfBroaCastReceiver.add(unique_id);
            Intent i = new Intent(context,
                    NotificationView.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("location", location);
            i.putExtra("location_title", location_title);
            i.putExtra("note", note);
            i.putExtra("total_route", total_route);
            context.getApplicationContext().startActivity(i);
    
        }
    
    }
    
    }
    

    并且在广播接收器启动的活动中

    public class NotificationView extends Activity {
    SettingSharedPreference pref;
    private static final String PROX_ALERT_INTENT = "com.routerapp.ProximityAlert";
    // Typeface type;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_location_leached_dialog);
    
        Button tBackbutton = (Button) findViewById(R.id.id_4_alert_back);
        TextView tTextNote = (TextView) findViewById(R.id.id_4_alert_note);
        TextView tTextTitle = (TextView) findViewById(R.id.id_4_alert_title);
        TextView tTextSubTitle = (TextView) findViewById(R.id.id_4_alert_subtitle);
        Bundle data = getIntent().getExtras();
    
        tTextNote.setText(data.getString("note"));
        tTextTitle.setText(data.getString("location_title"));
        tTextSubTitle.setText(data.getString(""));
        DrivingRouteActivity.is_route_end_automatically = true;
        pref = new SettingSharedPreference(this);;
        if(DrivingRouteActivity.is_last_point)
        {
            tBackbutton.setText(R.string.btn_end_route);
        }
    
    
        if (pref.getNotePopup()) {
    
        } else {
    
            finish();
    
        }
        tBackbutton.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                finish();
    
            }
        });
    
    
        //KILL THE SERVICE
        Intent intent = new Intent(this,
                 LocationUpdateBoundService.class);
                 stopService(intent);
    
    
    }
    
    }
    

    此外,还会存储应用程序的状态,以便在销毁时保持应用程序状态。

    但是当系统停止在后台运行我的应用程序时,我将无法接收广播。

    没有得到什么问题。

1 个答案:

答案 0 :(得分:0)

像java这样的Android有垃圾收集,如果要在其他地方使用大量内存,android会关闭应用程序。因此,有可能在谷歌地图启动期间引导您的用户Android操作系统关闭您的应用程序并将其从内存中删除以释放空间用于其他内容。

然而,如果没有任何代码,我无法再向您提供任何更多信息或建议。除了广播接收器启动和Intent打开您的应用程序,并确保应用程序被销毁时存储您的应用程序的状态,以便您可以在之前离开的地方启动它。