无法创建服务

时间:2015-06-06 19:05:07

标签: android service android-service

我是Android的新手,并在我的一个提醒应用程序中创建服务。但是当我在我的活动的onCreate()方法中调用startService()时,我的服务正在触发我无法看到在我的服务类的不同方法中使用的toast。作为android的新手无法找出问题。 的 MainActivity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_show_reminders);
        isReminders = false;
        dataSrc = new RemitDataSrc(this);
        dataSrc.open();

         rem = dataSrc.findALL();
        refreshDisplay();
    }
    private void refreshDisplay(){
        if(isMyServiceRunning(GeofencingService.class)){
            Intent intent = new Intent(this, GeofencingService.class);
            stopService(intent);
            Toast.makeText(this, "yes service", Toast.LENGTH_LONG).show();
        }else{
        Toast.makeText(this, "No service", Toast.LENGTH_LONG).show();
        }
        if(rem.size()==0){

            Toast.makeText(this, "No Reminders in Database", Toast.LENGTH_LONG).show();
        }


        Intent intent = new Intent(this, GeofencingService.class);
        startService(intent);
            ArrayAdapter<Reminders> remAdpt = new RemindersListAdapter(this, rem);
            setListAdapter(remAdpt);


    }
    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

GeofencingService:

public class GeofencingService extends Service{
    RemitDataSrc dataSrc;
    /**
     * Geofence Store
     */
    private GeofenceStore mGeofenceStore;
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate() {

        super.onCreate();
        Toast.makeText(this, "GeoFencing Service created", Toast.LENGTH_LONG).show();
        dataSrc = new RemitDataSrc(this);
        dataSrc.open();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "GeoFencing Service Started", Toast.LENGTH_LONG).show();
        serviceTest sT= new serviceTest();
        dataSrc.open();

        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "GeoFencing Service Stoped", Toast.LENGTH_LONG).show();
        dataSrc.close();
    }

}

的manifest.xml:

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SetReminderActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyD5GMap3cZBiMYWEiK0WX-2whx0xuqBwW4" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".AddReminderActivity"
            android:label="@string/title_activity_add_reminder" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityShowReminders"
            android:label="@string/title_activity_activity_show_reminders" >
        </activity>
         <activity
            android:name=".RemindersDetailActivity"
            android:label="Reminder Details" >
        </activity>
        <service 
            android:name=".LocationCheckerService"
            >

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

        </service>
    </application>

3 个答案:

答案 0 :(得分:3)

正如您在其中一条评论中提到的,服务包名称与包含活动的包不同,请在清单中使用包名称的完全限定类名。

<service 
android:name="com.afifa.projectreminder.service.GeofencingService" android:exported="false" 
> 
</service>

答案 1 :(得分:0)

请将您的服务注册为您的应用程序的子项,例如:

{{1}}

答案 2 :(得分:0)

在应用程序标记下的项目清单中提及该服务。 像这样

var debug=require('debug')('UserController');