in the manifest i have :
<activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" ... >
</activity>
<service android:name="me.myFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
and myFirebaseMessagingService is inside a dedicated jar
now the problem is that from myFirebaseMessagingService i want to launch an activity via intent. normally i must do like this :
import com.embarcadero.firemonkey.FMXNativeActivity;
Intent intent = new Intent(this, FMXNativeActivity.class);
however from inside the jar i don't have access to the code of FMXNativeActivity (because the jar is to be used in a generic way).
How can i do? can i set the activity name inside the <service ..></service> as params ?
Set an action for your activity in manifest and start the activity from service using that action.
Manifest.xml
<activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" ... >
<intent-filter>
<action android:name="com.example.foo.bar.YOUR_ACTION" />
</intent-filter>
</activity>
In your service,
String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);
startActivity(i)
Create a broadcast receiver that starts your service and when you want to invoke the service simply broadcast with appropriate action this way your broadcast receiver can start the service
Related
first question here...
I have this practice from my college, but the course sometimes doesn't explain everything, and it's about broadcast and receiving a Toast message.
(Some things are going to be in Spanish)
You see, the first app it's about having a view with a button:
Activity View
The only thing that it does is sending a message through a button, and the OnClick has linked this method that is on the Activity, which name is Emisora.java (there is no main activity, but it is configured to this be the launch activity):
public void Emision(View v){
Intent intent = new Intent();
intent.setAction("com.tecmilenio.practica91");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
The Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmilenio.practica91">
<application
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/AppTheme">
<activity android:name=".Emisora">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Then I have the other app, which doesn't have an activity but it does have an Broadcast Reciever:
package com.tecmilenio.receptor;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BroadcastReceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Transmission Recieved", Toast.LENGTH_SHORT).show();
}
}
(I know having a Toast for this is not the best practice but is just for the practice of the course)
And its Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmilenio.receptor">
<application
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/AppTheme">
<receiver
android:name=".BroadcastReceptor"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.tecmilenio.practica91"/>
</intent-filter>
</receiver>
</application>
</manifest>
And when I install and execute the first one it just doesn't happen anything, I tap the button but nothing happens.
Then I later saw that in the second project (the receiver) when I execute it from the Android Studio, the app doesn't appear in the phone, neither execute anything, but i thought this was normal due to the lack of an activity in it. Then I also saw that when executing through the studio also the Run console sends me the message "Timed out waiting for process (com.tecmilenio.receptor) to appear on xiaomi-mi_8-2ef63c6e.", maybe this has something to be (?)
I would appreciate that someone explains me this... Thanks!
Update
I have found that the problem has to be a lot with the Timeout on launch, adding an activity with nothing and launching that activity makes it work, however that is not what it's supposed to do, it is supposed that it shouldn't have any activity, and should execute the OnRecieve when the "practica91" app sends the message...
Is there any way of how to do this?
Set action declared in manifest and sending broadcast from an activity is different.
try using the same action at both the places -
Intent intent = new Intent();
intent.setAction("com.tecmilenio.practica91");
intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
I think there is a small mistake here. You're sending as an action in the first app
the following string com.tecmilenio.emision. Whereas in the second app you're registering the broadcast's action as below
<action android:name="com.tecmilenio.practica91"/>
The first change would be to rename the broadcast action to com.tecmilenio.emision.
<receiver
android:name=".BroadcastReceptor"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.tecmilenio.emision"/>
</intent-filter>
</receiver>
Another important point is that when sending the broadcast add FLAG_INCLUDE_STOPPED_PACKAGES flag to the intent because when you broadcast from app A to app B , app B might not be running, this flag insures that the broadcast reaches out even apps not running:
Regards I'm Mexican too!
I am building an app that sends a notification when wifi connection is established. Since I am using android 9 setting intent filter in AndroidManifest.xml doesn't work.
I created a custom BroadcastReceiver, that creates a notification, and it works if I register receiver in onCreate.
Here is the registerReceiver part of the code in onCreate (it works when app is opened)
receiver = new WifiStateReceiver();
IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(receiver, filter);
Here is my BroadcastReceiver:
public class WifiStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("TEST", "IT WORKS!");
}
}
And here is my AndroidManifest.xml
.
.
.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
.
.
.
<receiver
android:name=".WifiStateReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
.
.
.
After I close my app this doesn't work anymore. So I need some kind of service that never closes, but I'm not sure how battery inefficient this method is. Is there a way to run a service when wifi connection/state changes? And as I already said, putting intent-filter in AndroidManifest doesn't seem to work as I am using android 9.
I'm trying to create a simple BroadcastReceiver that can receive the android.intent.action.BOOT_COMPLETED intent as well as the android.intent.action.MEDIA_MOUNTED intent. The idea is to start a service on receiving either of these intents. Thus the service should be started after Android boot is complete or when an USB storage device is connected to the Android target(if the service is not already started by then).
Permissions used by the application are defined in this section
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
The following section defines the BroadcastReceiver responsible for handling the intents and starting the corresponding service.
<receiver
android:name="com.example.systemupgradeapplication.IntentReceiver"
android:label="USB Detection Receiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
<intent-filter android:priority="999" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
The corresponding service that should be started from the Broadcast Receiver is defined in the following section
<service android:name=".SysUpgradeService" />
Note: The receiver and service components are defined within the <application> section of the Android Manifest.
The following snippet is the Class responsible for handling the intents broadcasted
public class IntentReceiver extends BroadcastReceiver {
private final static String TAG = "IntentReceiver";
private static boolean m_UsbInserted = false;
private static boolean m_UsbRemoved = true;
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= ");
// TODO Auto-generated method stub
//if(intent.)
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
String path = intent.getDataString();
if(path.contains("usb")) {
m_UsbInserted = true;
Log.d(TAG, "LaunchReceiver::ACTION_MEDIA_MOUNTED :: intent received with path= "+path);
Intent myIntent = new Intent(context, SysUpgradeService.class);
myIntent.putExtra("path", path);
context.startService(myIntent);
}
}else if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d(TAG, "BOOT Completed intent received");
context.startService(new Intent(context, SysUpgradeService.class));
}
The problem I am facing is that none of the intents are arriving at my broadcast receiver(None of the Logs in the IntentReceiver class are being printed in logcat) even though I can see in the Android Debug logs that the BOOT_COMPLETE and MEDIA_MOUNTED intents are being broadcast. Also This application is not starting after android system is booting up.
I appreciate your help in this regard, what may be wrong with my approach and some possible solutions.
Okay, so I pushed the apk to /system/priv-app which is where System Applications which are part of custom ROM are placed. Now I do not need any activity in my application since it is part of the custom ROM and is recognized as a system application. It seems that if your application is a 3rd party application it must have an activity to be able to receive broadcasted intents.
However in this case I have control over the custom ROM source code as well as root access on the device. So both the approaches work
Make your application part of the custom ROM source, build and flash on device.
Get root access on device, push your apk to /system/priv-app (4.4 onwards), reboot and voila!
Make Sure you have atleat one activity present in your Application.From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.
<receiver android:name="com.BillManager.DateChanged">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED"/>
</intent-filter>
</receiver>
in class...
if (action.equals(Intent.ACTION_DATE_CHANGED))
{
Log.d("DateChanged","DateChanged");
}
Hello,
I am devloping an Android app and I use this broadcast to announce when the user changes the date manually.
For some reason I get the broadcast at 12:00 without changing the date manually.
Does anyone have an explanation why is it?
I know, I am not the first onbe with this problem, but I tried so many solutions, I have found and no one works... maybe you could find the error
The error (also came so without .class and with /.Client depending on other settings)
12-02 16:40:15.359: W/ActivityManager(74): Unable to start service
Intent { act=com.android.fh.EnOceanApp.Client.class }: not found
In the manifest, this is included in application, out of activities (tried it also in activities and with ".Client"
The code in onCreate()
startService(new Intent(this, Client.class));
or
startService(new Intent(this.getApplicationContext(), Client.class));
or
Intent intent=new Intent("com.android.fh.EnOceanApp.Client.class");
this.startService(intent);
or
Intent intent=new Intent("com.android.fh.EnOceanApp.Client");
this.startService(intent);
And now, I dont have an Idea anymore....
com.android.fh.EnOceanApp is the package, Client.java the service-class in this package
and the manifest I forgot:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".EnOceanAppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListView"
android:label="List View">
</activity>
<activity android:name=".GraphView"
android:label="Graph View">
</activity>
<service
android:name=".Client"></service> //with and without ., of course without this comment
</application>
Thanks to user njzk2 for letting me notice what was happening.
I've had the same problem. It seem that Android OS can't find the service class that you've requested if you haven't registered before in the manifest file of your proyect.
Remember that a service is like an activity but without graphic interface. It means that the services needs to be registered before you can use them
This is how you register the service in your Android project:
<application>
<!-- your code -->
<activity>
<!-- your code -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.your.own.service.class"></service>
</application>
Just Remember that YourService class needs to extend from Service, if not your class won't be a service.
public class YourService extends Service{}
Sometimes you'll need to fully qualify your class name in the manifest, rather than using the shortform (.classname). I've seen that when I used classes from a different package, but perhaps it would help here since the service intent may go outside of the app.
So.. just to eventually help others or not:
I made a new project, copied the sources and tried to run it: the service was found now.
What was the difference, or in other words: what do I think, might give problems:
the long package name or the beginning with com.android... In the new project I just chose com.enocean
Despite ALL the answers in this post and many related Unable to start service Intent: not found Unable to start Service Intent , I still struggled and it took some time for me to get this going. My scenario was slightly more complicated since I'm trying to start a service in a DIFFERENT app that the one I'm calling it with. I figured it out and here are ALL the details, along with some bonus code.
MainActivity of calling intent (or whereever)
Intent intent=new Intent("com.example.core.MusicService.1234");
//Or Intent intent=new Intent("com.example.core.MusicService.TOGGLE_PLAYBACK");
PendingIntent pendingIntent = PendingIntent.getService(this, 99, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Manifest: of Service (Service tag inside Application tag)
It's
<service android:name="com.example.core.MusicService">
<intent-filter>
<action android:name="com.example.core.MusicService1234"></action>
</intent-filter>
<intent-filter>
<action android:name="com.example.core.MusicService.TOGGLE_PLAYBACK"></action>
</intent-filter>
</service>
MusicService.java
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
if(intent.getAction() != null){
if(intent.getAction().contentEquals("com.example.core.MusicService.TOGGLE_PLAYBACK")){
//Do work here
}
}
}
}
Notes
Service does NOT need to be started, this intent will start it
"com.example.core.MusicService1234" and "com.example.core.MusicService.TOGGLE_PLAYBACK" can be whatever you want it to be, but obviously needs to match the intent-filter in the service manifest with the calling intent. You can put multiple of these so you can do different actions when your service starts depending on the value from your intent
99 can be whatever you want, but must be unique if you're using notifications
I'm not sure it's possible to call a service in a different app (like this) without using the intent-filter - if it is, someone please enlighten us. I tried and it doesn't work.
Credit to: the cumulative information from all the posts :)
I made the silly mistake of adding the tag to a separate in the manifest.
In that case, the current application was unable to find the service defined.
Hope you skip that mistake :)
Thanks.
It is stupid mistake of android
This will not work
<service
android:name=".classname"/>
But this will work, have separate closing tag
<service
android:name=".classname"></service>
Well, in my case i had to clean the project. It sometimes happens when you have made a new Java class for the service in your package/project but did not build/clean the project afterwords. In my case, i just had to clean the project to get rid of the error.
If anyone sees this and has the same problem that I did, it was because I followed a guide and used context.startService() instead of context.startActivity()