I know this question has been a lot of times already, but I tried, I think, all the accepted answers, but it didn't solve my problem. I might be missing something, being new to Services and these kind of intents.
I have two apps, and one of them needs to call a service implemented in the second one.
In my app being called, I declared this in manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.myapp">
<application
android:name=".MyApplication">
<service
android:name="com.myapp.SynchronizationService"
android:exported="true"
android:enabled="true" >
</service>
</application>
</manifest>
My service being declared as follows :
package com.myapp;
/**
* Sync service
*/
public class SynchronizationService extends Service {
#Override
public void onCreate() {
super.onCreate();
initSync();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void initSync() {
//do some work
}
}
And then, in my app calling the service, I have declared this :
val i = Intent().apply {
component = ComponentName("com.myapp", "com.myapp.SynchronizationService")
}
val c: ComponentName? = startService(i)
And in AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.intentcaller">
<application>
...
</application>
<queries>
<package android:name="com.myapp" />
</queries>
</manifest>
But when I try to call this, I get this error in logs :
Unable to start service Intent { cmp=com.myapp/.SynchronizationService } U=0: not found
Thanks
You can declare permission in the app with service and use this permission in the app when you start intent
Related
I'm developing an audio streaming application for Android and integrating Android Auto. I've been following these two tutorials.
Android Developer Training
PTR Android Blog
Using the Desktop Head Unit, I'm able to select my media app from the media app list, but from there a ProgressBar stays instead of giving way to the "To play something, open the menu at the top left." message seen in the Universal Music Player.
On inspection, it seems that the MediaBrowserServiceCompat's onGetRoot()is never invoked and thus never populating my MediaItemCompat into the Auto app's list.
My manifest contains the following.
<manifest package="com.app.audio"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:name="com.app.audio.AudioApp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.app.audio.presentation.home.HomeActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.app.audio.presentation.weather.WeatherActivity"
android:screenOrientation="userPortrait"/>
<activity android:name="com.app.audio.presentation.settings.SettingsActivity"/>
<activity android:name="com.app.audio.presentation.alarm.AlarmActivity"/>
<activity android:name="com.app.audio.presentation.sleep.SleepActivity"/>
<receiver android:name="com.app.audio.audio.AudioIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
<action android:name="android.media.AUDIO_BECOMING_NOISY"/>
</intent-filter>
</receiver>
<receiver android:name="com.app.audio.presentation.alarm.AlarmReceiver"></receiver>
<receiver android:name="com.app.audio.presentation.sleep.SleepReceiver"></receiver>
<service
android:name="com.app.audio.data.service.media.MediaService"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.gms.car.application"
android:resource="#xml/automotive_app_desc"/>
<meta-data
android:name="com.google.android.gms.car.notification.SmallIcon"
android:resource="#drawable/ic_launcher"/>
</application>
My automotive_app_desc.xml is very simple, only declaring Media.
<?xml version="1.0" encoding="utf-8"?>
<automotiveApp>
<uses name="media"/>
</automotiveApp>
My MediaService extends MediaBrowserServiceCompat. In the onCreate() I create and set my MediaSessionCompat.
#Override
public void onCreate() {
super.onCreate();
//...
mediaSession = new MediaSessionCompat(
this,
SESSION_TAG,
mediaIntentReceiver,
null
);
mediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(new MediaSessionCompat.Callback() {
#Override
public void onPlay() {
super.onPlay();
play(selectedStream);
}
#Override
public void onPause() {
super.onPause();
pause();
}
#Override
public void onStop() {
super.onStop();
stop();
}
#Override
public void onSkipToNext() {
super.onSkipToNext();
playNextStation();
}
#Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
playPreviousStation();
}
});
mediaSession.setActive(true);
setSessionToken(mediaSession.getSessionToken());
updatePlaybackState(ACTION_STOP);
}
Finally, the two overridden methods from MediaBrowserServiceCompat, of which neither is ever called.
#Nullable
#Override
public BrowserRoot onGetRoot(#NonNull String clientPackageName, int clientUid, #Nullable Bundle rootHints) {
return new BrowserRoot(ROOT_ID, null);
}
#Override
public void onLoadChildren(#NonNull String parentId, #NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
List<MediaBrowserCompat.MediaItem> items = getMediaItemsById(parentId);
if (items != null) {
result.sendResult(items);
}
}
As far as I can tell, that's everything required to get an Android Auto started, yet when I open the app on my desktop head unit, there is only a ProgressBar greeting me, and when I open the off-screen nav drawer, there's another one. I haven't heard of that state in any material I've read. Is there something I missed?
Ultimately, the issue didn't have anything to do with what I described. The aforementioned MediaService also does other tasks that require a custom Binder. This custom Binder didn't call the onGetRoot() needed for the Head Unit. As a solution, I check the Intent action and return super.onBind() when it's from the MediaBrowserServiceCompat.
#Override
public IBinder onBind(Intent intent) {
if (SERVICE_INTERFACE.equals(intent.getAction())) {
return super.onBind(intent);
}
return new MediaBinder();
}
The SERVICE_INTERFACE is a constant in MediaBrowserServiceCompat.
I'm developing an Android auto but I have some problems in this part of my code, in Onbind method of the service:
public IBinder onBind(Intent arg0) {
Log.i("TAG", "OnBind");
// TODO Auto-generated method stub
if (SERVICE_INTERFACE.equals(arg0.getAction())) {
Log.i("TAG", "SERVICE_INTERFACE");
registerReceiver(receiver, filter);
return super.onBind(arg0);
} else {
Log.i("Musica Service", "musicBind");
return musicBind;}
}
I have other activities bound with my service through a musicBind IBinder, but on the other hand I have set all things to connect my app in Android auto interface but after close my app after disconnect the device from the android auto I can't stop my mediabrowserservice compat. I think it's due to this SERVICE_INTERFACE keeps binded the service. How can I stop or destroy this from the same servicemediabrowserservicecompat?
Below is my code to capture notifications. I dont understand why the onNotificationPosted is not getting fired.
I am giving my app Notifications access from Settings > Security and the onCreate from MyNotifService is also getting fired, but onNotificationPosted is not getting fired.
What am I missing or doing wrong?
From my MainActivity's onCreate() function:
Intent intent = new Intent(this, MyNotifService.class);
this.startService(intent);
My Service code that extends NotificationListenerService:
#SuppressLint("NewApi")
public class MyNotifService extends NotificationListenerService {
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.v("focus", "in onNotificationPosted() of MyNotifService");
}
#Override
public void onCreate() {
super.onCreate();
Log.v("focus", "in onCreate() of MyNotifService");
}
}
In the manifest file, I have this:
<service android:name="com.mavdev.focusoutfacebook.notifications.MyNotifService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
It does seem to be a bit flakey, but this is the actual fix in this case:
#Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
I've been trying to get it working for the second day, and I succeeded. Try adding these lines at the end of onStartCommand method.
And, it hardly plays a role, but try to declare your service after activities in the manifest file.
This comment with sample code was really helpful to me: https://stackoverflow.com/a/41521664/10652152
The function "onReceive" is called when BroadcastReceiver is Registered in the Manifest but NOT called if registered dynamically.
The code that works is below:
public class EyeGesture extends BroadcastReceiver {
//Eye Gesture
private static IntentFilter eyeGestureIntent;
private static Context eyeGestureContext;
private static StringBuilder gestureInfo = null;
private static BroadcastReceiver broadcastReceiver;
// public void startEyeListening() {
//Eye Gesture
//}
#Override
public void onReceive(Context context, Intent intent) {
// this = context;
if (intent.getStringExtra("gesture").equals("WINK")) {
Log.e("WINKED ","");
}else {
Log.e("SOMETHING", "is detected " + intent.getStringExtra("gesture"));
}
//Disable Camera Snapshot
// abortBroadcast();
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntent = null;
eyeGestureContext = null;
gestureInfo = null;
}
}
Below is the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inno.inno.glassplugin" >
<uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainFunct"
android:icon="#drawable/ic_glass_logo"
android:label="#string/title_activity_main_funct" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data
android:name="com.google.android.glass.VoiceTrigger"
android:resource="#xml/voice_trigger" />
</activity>
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
</application>
</manifest>
The problem is that "onReceive" is NOT called when registered dynamically. I have to do this in a dynamic way.
Below is the code that is NOT working code.
public class EyeGesture extends Activity {
//Eye Gesture
IntentFilter eyeGestureIntentFilter;
Context eyeGestureContext;
BroadcastReceiver broadcastReceiver;
public EyeGesture(){
Log.e("CONSTRUCTOR ", "");
eyeGestureContext = MainFunct.getCurrentContext();
eyeGestureIntentFilter = new IntentFilter("com.google.glass.action.EYE_GESTURE");
eyeGestureIntentFilter.setPriority(1000);
startRunning();
}
void startRunning(){
eyeGestureContext.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Received ", " Something");
}
},eyeGestureIntentFilter);
}
#Override
public void onResume(){
super.onResume();
}
#Override
public void onPause(){
super.onPause();
unregisterReceiver(broadcastReceiver);
}
public void stopEyeListening() {
eyeGestureContext.unregisterReceiver(broadcastReceiver);
eyeGestureIntentFilter = null;
eyeGestureContext = null;
}
}
Also, I don't want to extend BroadcastReceiver from this class. Why am I not receiving anything if registered dynamically. I also removed the following line from the Manifest:
<receiver android:name="com.inno.inno.glassplugin.EyeGesture">
<intent-filter>
<action android:name="com.google.android.glass.action.EYE_GESTURE" />
</intent-filter>
</receiver>
but still, it is not working. There is no error or exception thrown.
What am I doing wrong?
Are you using explicit intent? It seems that dynamically registered broadcast receivers cannot receive explicit intents. Implicit intents work.
For reference: http://streamingcon.blogspot.com/2014/04/dynamic-broadcastreceiver-registration.html
If the issue is not explicit intents but if you are using LocalBroadcastManager for sendBroadcast then make sure that the registerReceiver is also called of LocalBroadcastManager and not of Context
Try using ApplicationContext instead of Activity.
Modyifing line:
eyeGestureContext = MainFunct.getCurrentContext();
I would try things in this order:
eyeGestureContext = getApplicationContext();
eyeGestureContext = getApplication();
If above does not work I would extend the Application and do:
public class MyExtendedApplication extends Application {
private static MyExtendedApplication instance;
public static MyExtendedApplication getInstance() {
return instance;
}
}
This works for me with global "android.net.conn.CONNECTIVITY_CHANGE" broadcast
Context c = MyExtendedApplication.getInstance();
c.registerReceiver(
connectivtyChangedReceiver,
connectivityFilter);
so should also for you with "com.google.android.glass.action.EYE_GESTURE"
Watching adb logcat in XE21.3, it looks like com.google.android.glass.action.EYE_GESTURE intent never hits the event bus; instead, it skips straight to com.google.glass.action.TAKE_PICTURE, which is the same intent as the camera button. So it looks like the eye-gesture API was removed without announcement.
The receiver should extend the BroadcastReceiver class.
Define the receiver in the manifest
In the code (maybe onCreate), register the receiver
Create a receiver object
Define the intent filters
call RegisterReceiver() passing in the receiver and the intent filters
I am working on an Application that requires to start a BackgroundService on Android Boot
This is the code that I am using to start the BroadcastReceiver on Android Boot
public class StartOnBootService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try
{
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.package.myApplicationPackage.BackgroundService");
context.startService(serviceIntent);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This is my BackgroundService.class
public class BackgroundService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
//code to execute when the service is first created
Toast.makeText(getBaseContext(), "BACKGROUND SERVICE STARTED", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
//code to execute when the service is shutting down
}
#Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
}
}
This is the error log that I managed to snipe from the CatLog while my Android was Booting.
12-21 10:28:01.279: E/EmbeddedLogger(1710): App crashed! Process: com.package.myApplicationPackage
12-21 10:28:01.289: E/EmbeddedLogger(1710): App crashed! Package: com.package.myApplicationPackage v1 (1.0)
12-21 10:28:01.289: E/EmbeddedLogger(1710): Application Label: myApp Label
My AndroidManifest.xml file
</service>
<service android:name=".BackgroundService">
<intent-filter>
<action android:name="com.package.myApplicationPackage.BackgroundService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartOnBootService"
android:enabled="true"
android:exported="true"
android:label="StartOnBootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
You Broadcast receiver name and android manifest receiver name is totally differ.
StartOnBootService or in manifest it is StartMyServiceAtBootReceiver
Maybe it's a good practice to look at the manifest file before rushing over stackoverflow.
I also had a similar problem, "crash after boot" and noticed that I had a wrong package name, as a result of a hasty copy paste action for one of my receivers.
<receiver android:name="com.somepackage.broadcast.ConnectionChangeReceiver" />
was indeed having a typo as
<receiver android:name="com.somepackage.broadcast.broadcast.ConnectionChangeReceiver" />
Correcting it obviously solved my problem.
I've read probably 100 questions and answers on this issue, but I cant seem to get this working. I'm trying to start a Service from an Activity. My manifest file seems OK, the way I'm starting the Service also seems to be correct. The following error is showing up in LogCat:
ActivityManager(1296): Unable to start service Intent
{ cmp=com.exercise.AndroidClient/com.client.Communication }: not found
I'm attempting to start the service by calling this in my Activity:
startService(new Intent(getApplicationContext(), Communication.class));
The Service is the following:
public class Communication extends Service {
public Communication() {
super();
}
#Override
public void onCreate() {
super.onCreate();
Log.i("Service", "Created service");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Service", "onStartCommand called");
return START_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
The entry in my manifest file is:
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidClient" android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/sms" android:label="#string/app_name" >
<activity> ... </activity>
<service android:enabled="true" android:name=".Communication" />
</application>
</manifest>
Any advice is greatly appriciated.
Where is the Communication class?
In your manifest you declare a service with android:name=".Communication", this means that your service class should be located in com.exercise.AndroidClient.Communication
Check that the packages are correct. Note that the "." (dot) refers to the root of your package (ie the package declared in the manifest). So, for example, if your package is com.exercise.AndroidClient and your service class is under com.exercise.AndroidClient.services.Communication you need to declare the service like this:
<service android:enabled="true" android:name=".services.Communication" />
Or specify the full package:
<service android:enabled="true" android:name="com.exercise.AndroidClient.services.Communication" />
I had the same error and the cause was, that the service was not defined in the AndroidManifest.xml.
Also, if your service happens to exist in a library project that you are refering to, check your project.properties file.
You have to add this line:
manifestmerger.enabled=true