Unable to start service Intent: not found

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

I've the same issue described here but i can't understand whats wrong.

My issue is: Unable to start service Intent { act=.connections.MoodyService } U=0: not found

EDIT I've changed my package from connections to service in the source code, sorry for the confusion

My manifest`

service is the package and MoodyService is the class name

My service class

public class MoodyService extends Service {  public MoodyService() {     // TODO Auto-generated constructor stub }  private boolean isRunning = false; Object getContent;  @Override public IBinder onBind(Intent intent) {     // TODO Auto-generated method stub      return null; }  @Override public void onCreate() {     super.onCreate(); }  @Override public int onStartCommand(Intent intent, int flags, int startId) {     super.onStartCommand(intent, flags, startId);      // Announcement about starting     Toast.makeText(this, "Starting the Demo Service", Toast.LENGTH_SHORT)             .show();      // Start a Background thread     isRunning = true;     Thread backgroundThread = new Thread(new BackgroundThread());     backgroundThread.start();      // We want this service to continue running until it is explicitly     // stopped, so return sticky.     return START_STICKY; }  @Override public void onDestroy() {     super.onDestroy();      // Stop the Background thread     isRunning = false;      // Announcement about stopping     Toast.makeText(this, "Stopping the Demo Service", Toast.LENGTH_SHORT)             .show(); }  private class BackgroundThread implements Runnable {     int counter = 0;      public void run() {         try {             counter = 0;             while (isRunning) {                 System.out.println("" + counter++);                 new Contents().getAll(getResources(),                         getApplicationContext());                 Thread.currentThread().sleep(5000);             }              System.out.println("Background Thread is finished.........");         } catch (Exception e) {             e.printStackTrace();         }     } } 

And in my main.

Intent start = new Intent(".service.MoodyService");         this.startService(start); 

and also tried

Intent intent = new Intent(this, MoodyService.class);         this.startService(intent); 

and tried with the full path

 

回答1:

Solved

I deleted the period in the beginning of the package name in the manifest and it worked, in another words:

This doesn't work:

.yourPackage.YourClass 

But this does work:

 yourPackage.YourClass 

And in the main:

Intent intent = new Intent(this, MoodyService.class);         this.startService(intent); 

But it goes against what is written in the documentation:

android:name The name of the Service subclass that implements the service. This should be a fully qualified class name (such as, "com.example.project.RoomService"). However, as a shorthand, if the first character of the name is a period (for example, ".RoomService"), it is appended to the package name specified in the element. Once you publish your application, you should not change this name (unless you've set android:exported="false").

There is no default. The name must be specified.



回答2:

The service must be also be included in the Manifest:



回答3:

I don't know why you are using that package-like name for your service name, but why don't you use class name for starting the service?

Intent intent = new Intent(context, YourService.class); context.startService(intent); 


回答4:

I think in manifest package name for service is wrong as you said your package name is connections so it should be like this

  android:name ="connections.MoodyService" 

or

  android:name="com.example.moody.connections.MoodyService" 

to invoke service do

  Intent intent = new Intent(this, MoodyService.class);     this.startService(intent); 


回答5:

Make sure you have declared your service in the manifest file.

and try writing getApplicationContext() instead of "this" keyword

startService(new Intent(getApplicationContext(), MoodyService.class)); 


回答6:

Did you create an empty constructor in the service?

If not, try that.

Also uncertain if you can use the Intent like that. You could try the 'new Inten(this, MoodyService.class)' construction.



回答7:

my service is in "service" package and my manifest service enrty like this;

     


回答8:

Did you try to use the android:name that you specified in the Manifest?

Android Manifest:

The call would be sth like this:

Intent intent = new Intent("UploadService"); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!