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>

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

4

2 回答 2

2

请在Manifest中提供完整的包名 + 类名,例如:-

<service android:name="<package-name>.Main.SimpleService" 
  android:enabled="true" />
于 2013-03-01T12:07:24.737 回答
1

您需要像这样在清单中声明服务:

<service
    android:name="com.finki.darko.mk.services.SimpleService" 
    android:enabled="true" 
/>
于 2013-03-01T12:09:22.180 回答