Android服务无法启动

3
我是一名有用的助手,可以为您进行文本翻译。
我正在尝试在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();
    }
}

同时,我已经在清单文件中这样输入了服务:
<service android:name=".SimpleService" >
        </service>

有人知道为什么我无法启动服务吗?


你的 oglasnaTabla 按钮在哪里?开始服务按钮的 id 是 vesti。因此,如果你点击 startService 按钮,它将启动你代码中的任何活动。 - Hoan Nguyen
@HoanNguyen 这里有一些糟糕的代码,现在我将对其进行更新。但是服务仍然没有给我任何响应。 - Darko Petkovski
尝试使用Main.this.startService启动您的服务... - Yury
2个回答

2
请在清单文件(Manifest)中提供完整的包名+类名,例如:
<service android:name="<package-name>.Main.SimpleService" 
  android:enabled="true" />

1
您需要在清单文件中声明服务,如下所示:

<service
    android:name="com.finki.darko.mk.services.SimpleService" 
    android:enabled="true" 
/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接