【问题标题】:missing android service though properly declared in code and manifest尽管在代码和清单中正确声明,但缺少 android 服务
【发布时间】:2014-01-24 11:26:05
【问题描述】:

我收到一个错误,好像找不到我正在使用的 LocationService 我错过了什么?

这是我的清单:

        <activity 
            android:name="com.example.manyexampleapp.StoresListActivity" >
                        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<service android:enabled="true" android:name="com.example.manyexampleapp.LocationService" />

我的 StoresListActivity 类:

package com.example.manyexampleapp;

public class StoresListActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        LocationService locService = new LocationService();

        locService.startService(new Intent());
    }

我的 LocationService 类

package com.example.manyexampleapp;

public class LocationService extends Service {
public static final String LOCATION_BROAD_MSG = "Hello World";
private static final int TWO_MINUTES = 1000 * 60 * 2;

public LocationManager locationManager;
public MyLocationListener listener;
public Location previousBestLocation = null;

Intent intent;
int counter = 0;

@Override
public void onCreate() {
    super.onCreate();
    intent = new Intent(LOCATION_BROAD_MSG);      
}

@Override
public void onStart(Intent intent, int startId) {      
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listener = new MyLocationListener();        
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
}

【问题讨论】:

    标签: java android android-intent nullpointerexception android-manifest


    【解决方案1】:

    你应该像这样启动一个服务。

    public void onCreate(Bundle savedInstanceState) {    
          super.onCreate(savedInstanceState);  
          Intent intent=new Intent("com.example.manyexampleapp.LocationService");  
          this.startService(intent);
    }
    

    并在您的 Android 清单文件中提及所需的权限,即

    <manifest ... >
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        ...
    </manifest>
    

    Here 是详细信息。

    【讨论】:

      【解决方案2】:

      尝试以这种方式启动您的服务:

      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
      
          startService(new Intent(this, LocationService.class));
      }
      

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        对于启动服务使用这个

         startService(new Intent(this, LocationService.class));
        

        代替

         LocationService locService = new LocationService();
        
         locService.startService(new Intent());
        

        【讨论】:

          【解决方案4】:

          请在 StoresListActivity 中检查您的意图对象。 ,即,

          locService.startService(new Intent());

          像这样触发你的服务:

          Intent i= new Intent(context, CustomService.class);
          
          context.startService(i); 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-08
            • 1970-01-01
            • 1970-01-01
            • 2018-03-05
            相关资源
            最近更新 更多