"Launching" another app with services - android

I have 2 different apps in 2 different Application Projects in Eclipse. I will call them H and G. Until now, I have been able to launch H from G by using
Intent intent = getPackageManager().getLaunchIntentForPackage("com.xxx.h");
startActivity(intent);
This has been working great, but in reality, I don't really need the H app to launch, I just need to do something with that app in the background, so I started looking into services. So now I am trying to use
Intent intent = new Intent();
intent.setClassName("com.xxx.h","com.xxx.h.MyService");
startService(intent);
But now I am getting the error saying
W/ActivityManager(1044): Unable to start service Intent { cmp=com.xxx.h/.MyService } U=0: not found
Very new to Services and even Intents so I am guessing it is something simple that I am missing and hoping you guys can help.
EDIT
I fixed the issue with is saying that I was unable to start service intent. That was fixed by including
service android:name=".MyService"
to the AndoirdManifest.xml in H. Now I am getting
E/AndroidRuntime(1022): java.lang.IllegalStateException: Could not execute method of the activity
EDIT 2
Found out it was not letting me start without permission so I had to include android:exported="true" in the AndroidManifest.xml as well

Try this:
private void startService(String aServiceName) {
if (aServiceName.trim().length() > 0) {
try {
Context ctx = getApplicationContext();
Intent iServiceIntent = this.ctx.getPackageManager().getLaunchIntentForPackage(aServiceName);
ctx.startActivity(iServiceIntent);
Thread.sleep(800);
} catch (Exception e) {
}
}
}

The initial problem of it being unable to start the service was because I did not include the <service> to the AndroidManifest.xml which fixed that issue, and then the could not execute error was caused by a permissions issue. So all in all, all it took to fix my issues was including
<service
android:name=".MyServiceHomework"
android:exported="true"
/>
To the AndroidManifest.xml of my H (the service) project

Related

How can I get a Xamarin forms app to auto start on Android 10?

I have put in place the following:
I added <receiver android:name="BootReceiver"></receiver> to application in the manifest XML file.
I added <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> to the manifest also.
I created a new class in the droid project called BootReceiver:
using Android.App;
using Android.Content;
using uarapp.droid;
[BroadcastReceiver(Enabled = true, DirectBootAware = true, Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent i = new Intent(context, typeof(MainActivity));
i.AddFlags(ActivityFlags.NewTask);
context.StartActivity(i);
}
}
The app does not start when the device boots. From Googling it looks like this process has changed for latest version of Android. Anybody know what needs to change? I can't find it online anywhere.
In case it matters the specific device I'm targetting is a RealWear HMT-1.
A lot has changed with Android 10 there are a lot of restrictions on starting something in the background more information here.
The possible solution is in the following answer on SO: https://stackoverflow.com/a/59421118/7462031
Possible Solutions:
1- You can choose just show a service notification, and start pending intent with a click
2- You can use full-screen intents to show your intent immediately as shown in the other answer and suggested by Google.
For full-screen intent solution, as described in the official document
The system UI may choose to display a heads-up notification, instead of launching this intent, while the user is using the device.
3- To start the activity automatically in the background, The most possible solution in my view is adding "SYSTEM_ALERT_WINDOW" to the manifest file. And ask for user permission once when the app opened the first time. (The user can give this permission manually - (Settings-Apps-Your App-Advanced- Draw over other apps))
I have also read about some brands restricting the above solutions so you might wanna investigate that too.
Good luck feel free to get back if you have queries.

Android: Unable to start service Intent { cmp=com.app.service/.NotificationService } U=0: not found

This question is related to the following:
Android start service of an app from another android app
What I am trying to do is:
I have two applications(both are installed but the second one is not started)
I start the first app and with it I start a service of the second app
Intent i = new Intent();
i.setComponent(new ComponentName("com.app.service", "com.app.service.NotificationService"));
context.startService(i);
The service from the second app should display a notification in the notification bar
The service is declared in the manifest file of the second app within the application tags:
<service android:name="com.app.service.NotificationService"
android:exported="true"
android:enabled="true">
</service>
The service does not start an I get the following message:
Unable to start service Intent { cmp=com.app.service/.NotificationService } U=0: not found
Do you have any suggestions on why this issue occurs?
Not exactly a great solution but I have fixed my problem by removing the NotificationService file from the com.app.service directory, and adding it to the com.app directory.
This fixed the issue for me, meaning that I managed to start the service of the second application from the first application.

How to find out the reason why Activity doesn't start

I got log cat message from startAnotherActivity() method
private void startAnotherActivity() {
Log.i(TAG, "Entered startAnotherActivity()");
Intent intent = new Intent();
intent.setAction(ANOTHER_ACTIVITY);
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
Another activity doesn't start, no other messages in log cat.
How can I resolve this issue?
UPDATE#1:
Sorry, I forgot to mention that AnotherActivity is an Activity in the other application, and therefore ANOTHER_ACTIVITY == 'some.other.app.domain.ANOTHER_ACTIVITY'
Shouldn't Dalvik complain if it cannot find specified activity?
One possible reason may be not declaring other activity in the manifest. You can do this like the following:
<activity android:name="your.package.your.activity">
</activity>
And then you can start the activity by doing the following:
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);
Hope this helps.
Since it's an activity in another application, you may need to set the component (fully qualified package name and fully qualified activity name).
See here: How to start activity in another application?
Or here:
Launch an application from another application on Android
Finally I found out my mistake.
In the project there are two similar messages in two activities, so I thought that runs one, but that was another.
Thank you for your assistance!

Cannot bind service from referenced library to another service

I am trying to bind a service from a library project that is added to another library project that is added to an application project.
So library A is referenced by library B and library B is added to my app. The app starts Service A. Another app starts Service B. Service B binds to service A but fails
ActivityManager(593): Unable to start service Intent { act=com.xx.yy.zz/.Service } U=0: not found
-I can find the jar containing the service in the application (in android dependencies).
-I declared the app in the manifest (application).
<service android:name="com.xx.yy.zz.Service"
android:exported="true"
android:enabled="true">
</service>
-The service declaration is within the application tag.
-The service extends Service
-Both services produce Logs so they are both started
This is how I try to bind the service
private void bindToService() {
Toast.makeText(getApplicationContext(), "Binding service", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClassName("com.xx.yy.zz", "com.xx.yy.zz.Service");
mBound = getApplicationContext().bindService(intent, this, BIND_AUTO_CREATE);
Log.d(TAG, "bindService returned " + mBound);
}
mBound always returns false.
The services have been binded before (in a testapp which uses allot of the same code).
the services bind using AIDL (nothing changed to that code).
I cant paste more code so I hope this is enough to get me on the way.
Thanks in advance.
I fixed it, it turns out i had to provide the application package name and the library package name.
So instead of:
intent.setClassName("com.library.package.name", "com.library.package.name.Service");
I had to do:
intent.setClassName("com.application.package.name", "com.library.package.name.Service");
I hope this helps someone else! I wasted allot of time on this.

Error Receiving Broadcast Intent Problem

I created a broadcast receiver in the main activity and the background service which is sending broadcast intents. The application crashes each time I try to run it and the Log displays the following error message:
10-04 13:30:43.218:
ERROR/AndroidRuntime(695):
java.lang.RuntimeException: Error
receiving broadcast Intent {
action=com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE
(has extras) } in
com.client.gaitlink.GaitLink$LoginStatusReceiver#431690e8
The broadcast message is sent from CommunicationService class in the following method:
private void announceLoginStatus(){
Intent intent = new Intent(LOGIN_STATUS_UPDATE);
intent.putExtra(SERVER_MESSAGE, mServerResponseMessage);
intent.putExtra(SESSION_STRING, mSessionString);
sendBroadcast(intent);
}
where
String LOGIN_STATUS_UPDATE = "com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE"
in the main activity the following broadcast reveiver is defined:
public class LoginStatusReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String serverMessage = intent.getStringExtra(CommunicationService.SERVER_MESSAGE);
String sessionString = intent.getStringExtra(CommunicationService.SESSION_STRING);
userInfo.setSessionString(sessionString);
saveSettings();
}
}
and registered in onResume method:
IntentFilter loginStatusFilter;
loginStatusFilter = new IntentFilter(CommunicationService.LOGIN_STATUS_UPDATE);
loginStatusReceiver = new LoginStatusReceiver();
registerReceiver(loginStatusReceiver, loginStatusFilter);
And the manifest file includes the following:
<activity android:name=".GaitLink"
android:label="#string/app_name">
<intent-filter>
...
<action android:name="com.client.gaitlink.CommunicationService.action.LOGIN_STATUS_UPDATE" />
</intent-filter>
</activity>
I would really appreciate if anyone could explain why the Log displays the message above and the application crashes.
Thanks!
I solved it by adding intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); when you start a new activity.
If not started from an activity, intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); is needed.
You have two Intent filters; you only need one. If you register the BroadcastReceiver via registerReceiver(), only use the IntentFilter that is the second parameter to that API call -- do not also put an <intent-filter> element in the <activity> in the manifest.
I do not know for certain if that is your problem, but it certainly does not help.
Have you gone step by step in the debugger to find exactly where the crash occurs?
One thing to look out for is if you are overriding any Android lifecycle events that you are properly calling the base class's constructor when necessary.
when you are looking at error logs, you are just looking at first few lines.
but surprisingly actual problem is mentioned in lines way below it-
Fatal Error : Main
error receiving broadcast...bla bla bla <- you are just looking here only
at x.y.z.....
at x.y.z......
at x.y.z.....
at x.y.z......
caused by : ........................... <- but actual problem is here!
at x.y.z.....
at x.y.z......
at x.y.z.....
at x.y.z......
I'm not sure that you can understand what I want to say, because of my English.
I think this line of code is cause of a problem:
userInfo.setSessionString(sessionString);
My project was wrong because I want to:
int i = Integer.parseint(intent.getExtars("9"));
and you register two times.
i think you have to use life cycle method onPause() and onResume() Method for unregister and register broadcast intent.
I found that making sure the intent was run from the original activity and putting things into one class got rid of the entire issue.
This is a very old question, but I think many people would still be searching answers for it. My situation is quite similar to this one.
What I wanted to do, is to call finish() in the child activity when certain events occur in parent activity i.e. by sending the broadcast message from MainActivity to Child Activity.
Following is the code in MainActivity when the event is occurred (e.g. when network connection is broken etc etc):
Intent intent = new Intent("ACTIVITY_FINISH");
intent.putExtra("FinishMsg","ACTIVITY_FINISH: Network broken.");
sendBroadcast(intent);
In the child activity's OnResume() function:
IntentFilter quitFilter = new IntentFilter();
quitFilter.addAction("ACTIVITY_FINISH");
registerReceiver(m_quitReceiver, quitFilter);
In the child activity's OnPause() function:
unregisterReceiver(m_quitReceiver);
Within the child activity class:
BroadcastReceiver m_quitReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, final Intent intent)
{
if (intent.getAction().equalsIgnoreCase("ACTIVITY_FINISH"))
{
Log.d("INFO", intent.getExtras().getString("FinishMsg"));
finish(); // do here whatever you want
}
}
};
Hope this helps.
It might because you keep registering BroadcastReceiver. I made that mistake before as a result it returns this error. Make sure BroadcastReceiver only registered once.
Ok what worked for me was declaring the onNotification method as follows:
window.onNotification = function(event) {
alert('onNotification called!');
};

Categories

Resources