2017-01-11 314 views
2

我必须获取人员的轨迹(例如从家到工作),因此我的应用程序获取了经度和纬度(我有两个按钮:1.开始获取纬度和经度2.停下来获取经纬度)。但是,我希望Android不会杀死应用程序,我想让应用程序在用户使用Facebook,Twitter(例如)时运行,或者只是用户锁定他的智能手机。 当用户在使用Facebook或Twitter时(例如)使用应用程序时,我的应用程序工作正常,但是当我锁定我的智能手机时,Android会终止应用程序。 我试图使用intentservice和服务,但他们不工作时,我锁定屏幕。我应该使用PowerManager.WakeLock吗?我不完全知道它是如何工作的以及它是如何工作的。保持应用程序始终在Android上运行在后台

这是我做了尝试服务,我不知道如果我错了一个例子,但是当它不工作: 1.我在应用程序(我已经开始服务) 2。我点击主页按钮(服务运行时) 3.我锁定屏幕。 4.然后,服务和应用程序是由机器人杀死(和服务没有完成做它的东西)

清单

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.javosoft.intentservice"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name=".MyService" 
      android:exported="false"></service> 

    </application> 

</manifest> 

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.javosoft.intentservice.MainActivity"> 

    <Button 
     android:text="start Service" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="63dp" 
     android:id="@+id/button" 
     android:onClick="startService"/> 

    <Button 
     android:text="Stop Service" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignLeft="@+id/button" 
     android:layout_alignStart="@+id/button" 
     android:layout_marginBottom="68dp" 
     android:id="@+id/button2" 
     android:onClick="stopService" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:hint="textito :D" 
     android:ems="10" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/editText" /> 
</RelativeLayout> 

Layout

MainActivity

package com.javosoft.intentservice; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 

    private Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (Button) findViewById(R.id.button); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this, MyService.class); 
       startService(intent); 
      } 
     }); 

    } 

    public void stopService (View view){ 
     Intent intent = new Intent(this, MyService.class); 
     stopService(intent); 
    } 
} 

为MyService类

package com.javosoft.intentservice; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.widget.Toast; 

public class MyService extends Service { 

    final class MyThreadClass implements Runnable{ 

     int service_id; 
     MyThreadClass(int service_id){ 
      this.service_id = service_id; 
     } 

     @Override 
     public void run() { 

      synchronized (this){ 
       int count = 0; 
       while (count < 10){ 
        Log.i("servicio", "while: " + String.valueOf(count)); 
        try { 
         wait(2000); 
         count++; 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
      stopSelf(service_id); 

     } 
    } 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "El servició ha empezado D:", Toast.LENGTH_SHORT).show(); 
     Log.i("servicio", "El servició ha empezado D:"); 


     Log.i("servicio", "onStartCommand arriba"); 

     Thread thread = new Thread(new MyThreadClass(startId)); 
     thread.start(); 

     Log.i("servicio", "onStartCommand abajo"); 
     return START_STICKY; 
     //return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onDestroy() { 
     //super.onDestroy(); 
     Toast.makeText(this, "El servicio ha padarado ._.", Toast.LENGTH_SHORT).show(); 
     Log.i("servicio", "El servicio ha padarado ._."); 
    } 

    //@Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 

    } 
} 

感谢

+0

呃,服务应该在关闭屏幕时起作用。 – Gudin

+0

它可以工作,如果我在应用程序,然后我锁定屏幕,但如果我在应用程序中,我点击主页按钮,然后锁定屏幕,Android杀死应用程序。 –

+0

你可以发布你的代码吗?你说你已经尝试了服务,但是你如何开始和结束服务很重要。发布开始和结束服务的代码以及Service的生命周期方法是很好的。我正在做你想用服务做什么,这样就可以完成。 – Gary99

回答

0

由于@Gudin说,这就像你在20秒后停止服务在我看来。但是,既然你说这有时候有效,我猜这只是一些测试代码。我怀疑你的问题在于你如何启动服务。传递活动的上下文意味着您的服务与该活动的生命周期相关联。如果这个被破坏了,我认为你的服务就是这样。 (只需通过活动&的onStop()而不是onDestroy()即可使服务完好无损)。试试这个

Intent intent = new Intent(getApplicationContext(), MyService.class); 

我试着重复你的问题,但不能,所以我不能肯定地说这是你的问题。这是一个开始的好地方。

1

试试我们这个

public class ForegroundService extends Service { 
    private static final String LOG_TAG = "ForegroundService"; 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     // Your logical code here 

     return START_STICKY; 
    } 

    @Override 
    public void onTaskRemoved(Intent rootIntent) { 

     //When remove app from background then start it again 
     startService(new Intent(this, ForegroundService.class)); 

     super.onTaskRemoved(rootIntent); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(LOG_TAG, "In onDestroy"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // Used only in case of bound services. 
     return null; 
    } 
} 

开始按钮点击:

Intent startIntent = new Intent(MainActivity.this, ForegroundService.class); 
    startService(startIntent); 

在清单

<service 
      android:name=".ForegroundService" 
      android:enabled="true" 
      android:stopWithTask="false" /> 
0

我读过,有些智能手机有这些类型的问题,我发现那华为,小米等有执行服务有问题,猜猜看:我的智能手机是华为。问题来自于这些设备上预装的程序,以节省能源。因此,我尝试在Nexus(此模拟器),Sony和Moto G上运行我的程序,该程序在这3款设备上运行良好。 感谢您的帮助。

相关问题