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.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moody"
android:installLocation="auto"
android:versionCode="0"
android:versionName="0.6.7 alpha" >
<uses-sdk
android:maxSdkVersion="18"
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="activities.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="activities.Menu_esq"
android:label="#string/title_activity_menu_esq" >
</activity>
<activity
android:name="activities.BaseActivity"
android:label="#string/title_activity_base" >
</activity>
<activity
android:name="activities.MainView"
android:label="#string/title_activity_main_view" >
</activity>
<activity
android:name="activities.LoginActivity"
android:label="#string/app_name"
android:noHistory="true"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.moody.LeftActivity"
android:label="#string/title_activity_left" >
</activity>
<activity
android:name="com.example.moody.RightActivity"
android:label="#string/title_activity_right" >
</activity>
<activity
android:name="activities.UserDetailsActivity"
android:label="#string/title_activity_user_details" >
</activity>
<activity
android:name="fragments.TopicsPreview"
android:label="#string/title_activity_copy_of_topics_preview" >
</activity>
<activity android:name="activities.Loading" >
</activity>
<service
android:name=".service.MoodyService"
android:icon="#drawable/ic_launcher"
android:label="#string/moody_service" >
</service>
</application>
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.
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
<service
android:name="com.example.moody.service.MoodyService"
android:icon="#drawable/ic_launcher"
android:label="#string/moody_service" >
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.
The service must be also be included in the Manifest:
<service android:name="com.x.x.serviceclass"></service>
Make sure you have declared your service in the manifest file.
<service
android:name=".MyService"
android:enabled="true" >
</service>
and try writing getApplicationContext() instead of "this" keyword
startService(new Intent(getApplicationContext(), MoodyService.class));
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);
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);
my service is in "service" package and my manifest service enrty like this;
<service
android:name="service.TimerService"
android:exported="false" >
</service>
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.
Did you try to use the android:name that you specified in the Manifest?
Android Manifest:
<service
android:enabled="true"
android:name="UploadService" />
The call would be sth like this:
Intent intent = new Intent("UploadService");
Related
I've built a radio app which plays live music from a stream with the help of MediaPlayer.
The problem started when I created a service which job is to execute a block of code when app is closed by the user(Its job is to delete the notification). In this service I am using startForeground() method in order to make it as a foreground service and therefore keep it alive as long as the app runs (because if I don't, the OS will kill it in 1 minute).
But the problem now is that when the user closes the app the MediaPlayer keeps playing. My app didn't have this behavior before adding the startForeground() method in the service.
I don't know if this would be helpful but if I add a System.exit(0) in the onTaskRemoved() method when app is closed by the user music stops and I get a notification from OS which says that my app is consuming battery.
Does anyone know why my application has this peculiar behavior? Why the application process isn't killed??
public class OnClearFromRecentService extends Service {
private static final String TAG = "onClearFromRecentServic";
private NotificationManagerCompat mNotificationManagerCompat;
#Override
public void onCreate() {
super.onCreate();
mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service Started");
startForeground(2001, new NotificationCompat.Builder(getApplicationContext(), CHANNELID)
.setContentTitle("title").setContentText("contentText").build());
return START_NOT_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service Destroyed");
}
#Override
public void onTaskRemoved(Intent rootIntent) {
//Put code here which will be executed when app is closed from user.
Log.d(TAG, "onTaskRemoved was executed ");
mNotificationManagerCompat.cancelAll();
stopSelf();
}
}
Manifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nikolouts.kwnstantinos.plutoradio">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"></activity>
<activity
android:name=".AboutActivity"
android:screenOrientation="portrait" />
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<receiver android:name=".MainActivity$NotificationReceiver" />
<activity android:name=".ChatActivity"/>
<service android:name=".OnClearFromRecentService" android:stopWithTask="false" />
</application>
</manifest>
You have in your manifest 'stopWithTask="false"'. This tells Android not to stop your Service when the user swipes the app from the list of recent tasks. Set this to "true"
I am trying to create an Android app that uses the pedometer. Currently having trouble getting the service to start up when the user restarts/turns their phone on and off. I am using 5.1, and testing has been done on a simulator. I am not sure if it is working since the logs have not been appearing in the console.
Any help is much appreciated!
App Manifest Class:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="app.apphub.devon.walkingquest">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".WalkingQuestSplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootReceiver" android:exported="true" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filtering>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filtering>
</receiver>
<service android:name=".StepCounterSensorRegister" android:enabled="true" android:exported="true"/>
</application>
</manifest>
Boot Receiver Class:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i("BROADCAST", "WALKING_QUEST_BOOT");
Intent _intent = new Intent("app.apphub.devon.walkingquest.StepCounterSensorRegister");
intent.setClass(context, StepCounterSensorRegister.class);
context.startService(_intent);
//context.startService(_intent);
//Toast.makeText(context,"Airplane mode on",Toast.LENGTH_LONG).show();
}
else {
Log.i("FAIL","BROADCAST FAILED");
}
}
StepCounterSensorRegister Class:
public class StepCounterSensorRegister extends Service implements SensorEventListener {
SensorManager sensorManager;
Sensor sensor;
long globalSteps;
boolean flag = false;
#Override
public void onCreate(){
}
public int onStartCommand(Intent intent, int flags, int startId) {
//Must run in background thread
Log.i("START","STEP_COUNTER_STARTED");
registerSensor(sensorManager.SENSOR_DELAY_UI);
return START_STICKY;
}
public void onSensorChanged(SensorEvent event) {
if(!flag){
}
if(event.sensor.getType() == Sensor.TYPE_STEP_COUNTER){
Log.i("STEPS DETECTED",""+globalSteps);
globalSteps++;
}
}
public boolean registerSensor(int accuracy) {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
try {
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
}catch (Exception e){
Log.i("ERROR", e.getMessage());
}
if(sensor == null) {
Log.i("FAILED","Sensor returned null");
return false;
}
else {
Log.i("SUCCESS", "Successfully registered sensor");
sensorManager.registerListener(this, sensor, accuracy);
return true;
}
}
Two things I see is that you declared something wrong in your manifest:
<receiver android:name=".BootReceiver" android:exported="true" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filtering>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filtering>
</receiver>
intent-filtering doesn´t exist, it is intent-filter
the permissions android:permission="android.permission.RECEIVE_BOOT_COMPLETED" is misplaced here, don´t put it inside receiver tag. You have already defined it above.
And be sure you set the correct filter in your Logcat, you are using two different tags "BROADCAST" and "FAIL", maybe also this is going wrong.
I am trying to start an activity when am restart my phone then its open app or show me toast when booting is complete
class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyIntentService.class);
context.startService(serviceIntent);
}
}
}
this is my Broadcaste receiver Code
class MyIntentService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
This is my service Code.
Manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyIntentService"></service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
am trying lots of diffrent code but no one work for me so can anyone help me to correct this code
Your BroadcastReceiver will never get called because you have this in the manifest entry for it:
android:exported="false"
Remove that.
NOTE: You also need to make sure that your app is started at least once manually after installing it on the phone. Otherwise your BroadcastReceiver will NOT get the BOOT_COMPLETE Intent.
NOTE: Also, using Toast as a debugging aid isn't a very good idea. You should write messages to the logcat and use that to determine if your Service is getting started, etc. Toast is not reliable as a debugging tool.
Add this in BroadcastReceiver class
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, SyncData.class);
context.startService(pushIntent);
Log.e("BroadCast Received", "ON BOOT COMPLETE");
}
}
and remove this two lines android:enabled="true"
android:exported="false"
in the below code i am trying to start a service as shown. but at run time
serviceCtrl.isMyServiceRunning()
returns false and the toast doesnt show up despite the service is added to the manifest file.
i am starting the service wrongly?! why the service does not start
code:
if (mIsBonded) {
mSB.append("Pairing Completed" + "\n");
Intent intSPPService = new Intent(getApplicationContext(), SPPService.class);
intSPPService.putExtra("key", "starting SPP Service");
startService(intSPPService);
ServiceCtrl serviceCtrl = new ServiceCtrl(getApplicationContext(), ActMain.class);
if (serviceCtrl.isMyServiceRunning()) {
Toast.makeText(getApplicationContext(), "Starting SPP Service", Toast.LENGTH_SHORT).show();
}
//mATRFCConn = new ATRFCConn();
//mATRFCConn.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mSB.append("Pairing Failed" + "\n");
}
...
...
...
public boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) this.mCtx.getSystemService(this.mCtx.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
service:
public class SPPService extends Service {
private final String TAG = this.getClass().getSimpleName();
final static String SPP_SERVICE_ACTION = "SPP_ACTION";
#Override
public void onCreate() {
Log.w(TAG, "onCreate");
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "onStartCommand");
String str = intent.getStringExtra("key");
Log.v(TAG, "Act->Service : " + str);
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
Update
at run time i receive
W/ActivityManager: Unable to start service Intent {
cmp=com.example.com.myapplication/com.example.com.servicebt_01.SPPService
(has extras) } U=0: not found
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.servicebt_01" >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".ActMain" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<service
android:enabled="true"
android:name=".SPPService"
android:icon="#mipmap/ic_launcher"
android:label="#string/service_name"
>
</service>
Please check this answer :
Unable to start service Intent
It says you have not given the name of the service in the manifest correctly. Please verify it.
Try to enter the Service name along with the entire package name in which it is present.
Your service name in manifest is error,not
<service
android:name="SPPService"
android:icon="#mipmap/ic_launcher"
android:label="#string/service_name"
>
</service>
If your SSPService is located in your application package,you can specify it like this:
<service
android:name=".SPPService"
android:icon="#mipmap/ic_launcher"
android:label="#string/service_name"
>
</service>
If not,you should specify its full path.
I try to play with Service in Android, my application contain an Activity with a button so that it can do action when I press it.
The core component is the ExtractingURLService , which extends IntentService framework class.
I have overrided the onHandleIntent() method, so that it can handle intent passed to it properly.
Code:
public class ExtractingURLService extends IntentService {
private static final String TAG = "ExtractingURLService";
public ExtractingURLService(String name) {
super("ExtractingURLService");
}
#Override
protected void onHandleIntent(Intent intent) {
// do something here !
}
}
}
The Android Manifest file is :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.service.urlextraction"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".ServiceExample02Activity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ExtractingURLService" android:enabled="true"></service>
</application>
</manifest>
In my only Activity class, I call the service by using:
Intent startServiceIntent = new Intent(this, ExtractingURLService.class);
startService(startServiceIntent);
That's all my work, but when deploy, the runtime error I received was:
04-09 16:24:05.751: ERROR/AndroidRuntime(1078): Caused by:
java.lang.InstantiationException:
example.service.urlextraction.ExtractingURLService
What I should do with that ??
Add full path of Service
<service android:name="com.foo.services.ExtractingURLService" android:enabled="true"></service>
Intent Service should look like this:
package com.foo.services;
import android.app.IntentService;
import android.content.Intent;
public class ExtractingURLService extends IntentService {
private static final String TAG = "ExtractingURLService";
public ExtractingURLService() {
super("ExtractingURLService");
}
#Override
protected void onHandleIntent(Intent intent) {
// do something here !
}
}
You can add try and catch block to avoid crash.Its work for me
try {
Intent intent = new Intent(DashboardActivity.this, MyService.class);
startService(intent);
}catch (IllegalStateException e){
}catch (Exception ee){
}