【问题标题】:Using Android Services, onStartCommand being accessed at a weird time使用 Android 服务,onStartCommand 在一个奇怪的时间被访问
【发布时间】:2014-11-20 23:13:25
【问题描述】:

我正在尝试创建一个应用程序,该应用程序可以在拍摄照片时为我提供 GPS 坐标。因为 GPS 需要准确,所以我将其作为服务运行以获得最佳锁定。但是,当打开主要活动时,onCreate() 中的startservice() 不会访问 GPSClass 中的 onStartCommand。

它运行onStart(),然后由于某种原因在完成两个启动过程后触发访问该类。

为什么会这样?这似乎太不合逻辑了。顺便说一句,我一直在尝试使用调试。

主要活动启动代码

public class MainActivity extends ActionBarActivity {

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

    Intent GPSstart = new Intent(this, GPSClass.class);
    this.startService(GPSstart);

}

private void GPSrunner() {
    Intent GPSstart = new Intent(this, GPSClass.class);
    this.startService(GPSstart);

    // check GPS is onstart
    if (!GPSClass.isGPSon()) {
        buildAlertMessageNoGps();
    }
}

  @Override
  protected void onStart() {
    super.onStart();
    GPSrunner();

  }

服务代码

public class GPSClass extends Service implements LocationListener {

private static final int TWO_MINUTES = 1000 * 60 * 2;
private static LocationManager locationManager;
private static boolean GPSOn;
private static Location location;

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    GPSOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    Toast.makeText(getApplicationContext(),"Service started",Toast.LENGTH_SHORT).show();
    return START_STICKY;

}

@ Override
public void onDestroy() {
    locationManager.removeUpdates(this);

public static boolean isGPSon() {
    return GPSOn;
}

清单

<service android:name=".GPSClass" 
        android:exported="false"
        />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【问题讨论】:

    标签: android service gps


    【解决方案1】:

    您不应该在清单中声明您的服务吗?

    <application
    
      .
      .
      .
    
        <service
            android:name=".MyService"
            android:label="My Service" >
        </service>
    </application>
    

    【讨论】:

    • 抱歉,我只是在发布问题时没有打印该部分,我可以让服务正常工作,它只是在 onCreate 和 onStart 中的所有调用发生后才正常工作
    • @emmistar 你为什么在 onCreate 和 onStart 中都使用?
    • 我正在使用 onStart() 来判断活动是否重新启动,以便再次打开 GPS。我实际上可能会在以后改变它。
    • @emmistar 尝试在服务中使用 onCreate 方法而不是 onStartCommand,就像在这个例子中 stackoverflow.com/questions/8828639/…
    • @emmistar 或者看看这个例子:androidhive.info/2012/07/android-gps-location-manager-tutorial也许可以帮助你
    【解决方案2】:

    我已经做了很多研究,这是我得到的最佳答案。

    Android 活动在完成 onCreate 和 onStart 命令后启动服务,因此我找到了另一种方法来收集我的项目所需的信息。

    这个结论得到了我发现的另一篇帖子的支持 Android Service never appears to start - onStartCommand() not called

    【讨论】:

      猜你喜欢
      • 2018-01-02
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2012-12-05
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多