无法创建服务
Unable to create service
我是 android 的新手,正在我的一个提醒应用程序中创建服务。但是当我在我的 activity 的 onCreate() 方法中调用 startService() 时,我的服务被触发我无法看到在我的服务的不同方法中使用的 toasts class.As new to android 无法找出问题所在。
主要活动:
@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;
}
地理围栏服务:
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>
请将您的服务作为应用程序的子项注册到您的清单中,例如:
<application ..>
..
<service
android:name=".GeofencingService"
android:enabled="true">
</service>
</application>
在应用程序标签下的项目清单中提及该服务。
像这样
<service android:name=".ServiceName" />
正如您在其中一条评论中提到的,服务的包名称与包含 activity 的包不同,请在清单中使用完全限定的 class 名称和包名称。
<service
android:name="com.afifa.projectreminder.service.GeofencingService" android:exported="false"
>
</service>
我是 android 的新手,正在我的一个提醒应用程序中创建服务。但是当我在我的 activity 的 onCreate() 方法中调用 startService() 时,我的服务被触发我无法看到在我的服务的不同方法中使用的 toasts class.As new to android 无法找出问题所在。 主要活动:
@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;
}
地理围栏服务:
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>
请将您的服务作为应用程序的子项注册到您的清单中,例如:
<application ..>
..
<service
android:name=".GeofencingService"
android:enabled="true">
</service>
</application>
在应用程序标签下的项目清单中提及该服务。 像这样
<service android:name=".ServiceName" />
正如您在其中一条评论中提到的,服务的包名称与包含 activity 的包不同,请在清单中使用完全限定的 class 名称和包名称。
<service
android:name="com.afifa.projectreminder.service.GeofencingService" android:exported="false"
>
</service>