Question

I want to start a service on click of option menu item.

 public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add("Help").setIcon(R.drawable.HelpActivity).setIntent(new Intent(this,HelpActivity.class));//this is activity intent .It works fine
    menu.add("Update Media").setIcon(R.drawable.update_icon).setIntent(new Intent(this,MediaUpdate.class));//this is service intent.It dose not works

return super.onCreateOptionsMenu(menu);
}

Manifest

    </service>
            <service android:name="com.Dexter.otp.services.MediaUpdate" >
    </service>

On click on update Media I get log error

11-30 12:46:10.775: E/MenuItemImpl(4918): Can't find activity to handle intent;   ignoring
11-30 12:46:10.775: E/MenuItemImpl(4918): android.content.ActivityNotFoundException:  Unable to find explicit activity class   {com.Dexter.otp/com.Dexter.otp.services.MediaUpdate}; have you declared this activity in your AndroidManifest.xml?

It seems like service/broadcast cannot be started by menu item.

Was it helpful?

Solution

If that does not work you can also try setting onclicklistener and call startService

menu.add("setting").setOnMenuItemClickListener(
            new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem arg0) {
                    [Activtyname].this.startService(new Intent(this, MediaUpdateService.class));
                    return false;
                }
            });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top