Android服务无法启动(Android service would't start)
我正在尝试在Android中实现简单的服务,但我无法统计基本服务。
这是我的主要课程:
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.finki.darko.mk.services.SimpleService;
public class Main extends Activity implements OnClickListener {
private Button startService;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if (!isOnline()) {
Toast.makeText(this, R.string.internetConnection,
Toast.LENGTH_SHORT).show();
finish();
} else {
setContentView(R.layout.main_activity);
startService = (Button) findViewById(R.id.vesti);
startService.setOnClickListener(this);
}
}
private static boolean isOnline() {
try {
InetAddress.getByName("google.com").isReachable(3);
return true;
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.vesti:
startService(new Intent(Main.this,SimpleService.class));
break;
}
}
}
and this is my simple server class:
ublic class SimpleService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
}
我也在清单中输入了这样的服务:
有人知道为什么我无法启动这项服务吗?
I'm trying to implement simple service in android but I could not stat the basic service.
Here is my main class:
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.finki.darko.mk.services.SimpleService;
public class Main extends Activity implements OnClickListener {
private Button startService;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if (!isOnline()) {
Toast.makeText(this, R.string.internetConnection,
Toast.LENGTH_SHORT).show();
finish();
} else {
setContentView(R.layout.main_activity);
startService = (Button) findViewById(R.id.vesti);
startService.setOnClickListener(this);
}
}
private static boolean isOnline() {
try {
InetAddress.getByName("google.com").isReachable(3);
return true;
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.vesti:
startService(new Intent(Main.this,SimpleService.class));
break;
}
}
}
and this is my simple server class:
ublic class SimpleService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
}
}
also I've entered the service in the manifest like this:
Does anybody know why I'm not able to start the service?
原文:https://stackoverflow.com/questions/15156557
更新时间:2020-01-27 19:31
最满意答案
您需要在清单中声明服务,如下所示:
android:name="com.finki.darko.mk.services.SimpleService"
android:enabled="true"
/>
You need to declare the service int the manifest like this:
android:name="com.finki.darko.mk.services.SimpleService"
android:enabled="true"
/>
2013-03-01
相关问答
这里是AutoStart应用程序的完整示例 AndroidManifest档案 <?xml version="1.0" encoding="utf-8"?>
package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
android:permission="
...
发生这种情况是因为活动B已经附加到正在运行的任务(根植于活动A中的任务)。 它也在FLAG_ACTIVITY_NEW_TASK的文档中指定: 使用此标志时,如果任务已在您正在启动的活动中运行,则不会启动新活动; 相反,当前任务将简单地以其最后一个状态被带到屏幕的前面。请参阅FLAG_ACTIVITY_MULTIPLE_TASK以获取禁用此行为的标志。 This happens because Activity B already is attached to a running task (the
...
您必须重写Service.onStartCommand(Intent, int, int) ,但不知何故,您只实现了onStartCommand(Intent, int, int, Location) ,该方法不属于Service的生命周期,因此从不自动调用。 您应该将Location location声明为本地变量而不是参数,并使用onStartCommand()注释onStartCommand()以在签名不匹配时获取警告。 You have to override Service.onStar
...
意图是系统的消息。 在这里,您要告诉它将LocationService类作为服务启动。 为此,您需要提供要启动的服务类: LocationService.class Intent service = new Intent(this, LocationService.class);
this.startService(service);
An intent is a message to the system. Here you want to tell it to start your Loca
...
我找到了解决方案,所以与大家分享。 有两种服务类型:一种是开始和停止。 这可以在应用程序中仅启动和停止一次。 其他您根据需要绑定和取消绑定N次。 我的服务属于第二种类型。 但是绑定和解除绑定并不能完成这项工作。 首先需要启动服务,然后才能绑定和解除绑定。 所以在启动应用程序或适当时,启动服务。 然后在需要时绑定。 当与它一起时,解开它。 绑定 - 解除绑定圈可以继续。 最后,当你确定你不需要它或在应用程序结束时停止服务。 所以流程就像开始 - >绑定 - >解除绑定 - >停止< - 希望这可以帮
...
尝试使用此代码进行服务类。 您不需要在单独的线程中创建MediaPlayer对象。 player.prepareAsync(); 已经是异步的。 编辑 : 进入AndroidManifest.xml的RadioService类的路径应该是绝对的: android:name="com.po.portelaonline.service.RadioService" public class RadioService extends Service implements OnPreparedListene
...
您需要在清单中声明服务,如下所示:
android:name="com.finki.darko.mk.services.SimpleService"
android:enabled="true"
/>
You need to declare the service int the manifest like this:
android:name="com.finki.darko.mk.services.SimpleService"
...
定义服务时,您的包名称不正确。 在Androidmanifest.xml中更改Service声明,如下所示:
android:name="com.example.test43.ProximityService" />
Your package name is incorrect when defining the service. Change you Service declaration in Androidmani
...
您没有正确注册该服务。 删除android:service in service行替换你的清单。 <?xml version="1.0" encoding="utf-8"?>
package="com.distorted.flashnotifier"
android:versionCode="0"
android:versionName="0.6" >
...
只有当窗口小部件被初始化(例如放在主屏幕上)或者updatePeriodMillis过期时,才会执行“onUpdate” - 方法。 如果您想通过单击窗口小部件来执行服务,则必须“附加”一个待处理的意图,如下所示: @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final Intent intent = new Intent(conte
...