Service in a global process in not getting created - android

I am creating a service and there will be multiple applications talking to the service. So I want the service to run in a global process i.e. process is not private to any application.
Here are the contents of my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.username.servicedemo" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
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="com.example.username.servicedemo.FirstService"
android:process="com.example.username.servicedemo.remote"
android:enabled="true"
android:exported="true" >
</service>
</application>
the process attribute under service will create a global process.
the problem is that when I try to start this service from activity on a button click then onCreate method in service in not getting called. Here is the code that executes on button click
public void startService (View view) {
int id = android.os.Process.myPid();
Log.i("MYTAG", " process id activity" + Integer.toString(id)
);
Intent intent = new Intent(this,FirstService.class);
intent.putExtra("key","1234");
ComponentName componentName = this.startService(intent);
Log.i("MYTAG",componentName.toString());
}
What am I missing here? Thanks!

It was a stupid mistake. The service was getting created properly. I was checking the log in onCreate method but the logs were filtered to be shown only from selected application. Since the service was running in a separate process the logs from service were not appearing in the logcat.
I just changed needed to change the filter. Putting a screenshot here in case it helps anyone.

Related

Intent-filter not working when the app is closed

I'm making an app that shows a notification each time the screen is unlocked. To that purpose, I have made a BroadcastReceiver that starts a Service. Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="android.example.example">
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="example"
android:largeHeap="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:hardwareAccelerated="true"
android:label="example" />
<receiver android:name=".RestartNotifications"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"/>
</application>
</manifest>
The broadcast receiver is really simple and looks like this:
public class RestartNotifications extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}
}
I have added a Toast to check if the error is due to not receiving the broadcast or due to an error in the service. When I install the app, it works fine and the notification appears every time I unlock the phone even if another app is opened and mine is stopped. However, if I kill my app, when I unlock the phone, the notifications don't appear anymore.
you need to run the receiver on a Service.
a simple receiver can't receive data if the app is closed
check this question

My app can't receive BOOT_COMPLETED broadcast

I am working on an app which needs receive BOOT_COMPLETED broadcast when the device boots.
According to the documents in Android Developers, I already know that since android 3.1, apps that never run after installing or force killed by user will not receive the boot broadcast when device boots, so with the rules, how can I receive boot broadcast? By the way, I have already tried open my app when boot is finished, but it is still not working.
Here's the code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.guoyonghui.todo"
android:installLocation="auto">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".tasks.TasksActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".taskdetail.TaskDetailActivity"/>
<activity android:name=".addedittask.AddEditTaskActivity"/>
<activity android:name=".statistics.StatisticsActivity"/>
<receiver android:name=".alarm.AlarmReceiver">
<intent-filter>
<action android:name="com.guoyonghui.todo.alarm.ACTION_TASK_ALARM"/>
</intent-filter>
</receiver>
<receiver
android:name=".alarm.BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(context, action, Toast.LENGTH_SHORT).show();
Log.d("AlarmReceiver", action);
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
TasksRepository tasksRepository = TasksRepository.getInstance(TasksLocalDataSource.getInstance(context));
List<Task> tasks = tasksRepository.loadTasks();
for (Task task : tasks) {
if (task.isActive()) {
AlarmReceiver.setAlarm(context, task);
}
}
}
}
}
You don't want to use the Default category in this case. According to the documentation of this category, it is used for the following purpose:
Set if the activity should be an option for the default action (center press) to perform on a piece of data. Setting this will hide from the user any activities without it set when performing an action on some data. Note that this is normally -not- set in the Intent when initiating an action -- it is for use in intent filters specified in packages. (Emphasis mine)
See the list of categories and what they're for here:
https://developer.android.com/reference/android/content/Intent.html

BootReceiver doesen't work

I know this has been asked tons of times, and there are hundreds of example on internet, but i want to understand what's wrong in my code.
As the title suggest i want to execute some code while the phone turn on, specifically i want to set some Alarms for getting notifications, but that's not relevant now, my problem is that the boot receiver onReceive method is never called apparently
I have the right permissions in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And i've also registered the receiver in the manifest
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And I've already created the receiver class
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context pContext, Intent intent) {
Toast.makeText(pContext,"waiting for debugger",Toast.LENGTH_LONG).show();
android.os.Debug.waitForDebugger();
//Stuff for the alarms
}
}
Can someone explain me what i'm a failing without posting always the same examples that i see everywhere?
I want to know what's wrong in my code, not how it should be done.
PS: : I forgot to say that i need to stop the code for debugging the alarm things, but i don't think that's the problem since it doesen't even show the Toast.
UPDATE: full manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="feddycapdev.conapo.turnario" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Calendario"
android:label="#string/title_activity_calendario"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Settings_Activity"
android:label="#string/title_activity_settings_" >
</activity>
<activity
android:name=".SettingGiorno"
android:label="#string/title_activity_setting_giorno" >
</activity>
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".WebNotificChecker" />
<service android:enabled="true" android:name=".Sveglia" />
<service android:enabled="true" android:name=".NotificaVigilanza" />
</application>
</manifest>
I should run the activity before the receiver?
Something has to use an explicit Intent to start one of your application's components before any manifest-registered receivers will work. For 99% of Android apps, that means that the user has to start your app from the home screen. If your app is serving as a plugin to some other app, you may not need the activity — please discuss this with the developers of the app that would be hosting your plugin.
how can I set notification if the user doesn't open activity?
You wouldn't set the notification if the user does not open the activity. Your app will only run when the user lets you run. If the user chooses not to start your app, or if the user chooses to "Force Stop" your app from within Settings, your manifest-registered receiver will not receive broadcasts.

Android application without GUI

I have been developing a simple application without UI using broadcast receiver.
The app doesn't contain any ACTIVITIES.
I have given necessary permissions.
I took the code from this url:http://developerandro.blogspot.in/2013/09/check-internet-connection-using.html
The app shows a toast "Not connected to internet" when I click change wifi state. It's working correctly.
But my question is There is an activity registered in my manifest file which I don't have. So I delete those lines from my manifest. Then no toasts are shown and I checked the logs too. No output on changing wifi state.
Why this happened? Please help me guys...
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast_internetcheck"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.broadcast_internetcheck.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.broadcast_internetcheck.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my Broadcastreceiver class:
public class NetworkChangeReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
/*Above line will return the status of wifi */
Toast.makeText(context, status, Toast.LENGTH_LONG).show();
}
}
You will need to create a dummy activity for a Service which will be triggered in the onCreate() of the dummy, maybe a non-UI with finish() .
Without that the required implementation is not possible, esp above Android 3.1.
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
Run only a background service when application start
Start android application without activity
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
And for more on Service:
http://developer.android.com/guide/components/services.html
https://developer.android.com/training/run-background-service/create-service.html
http://www.vogella.com/tutorials/AndroidServices/article.html
You can use service instead. But showing Toast through service bit complicated instead you can show notification through service for No Internet Connection.
If you don't want any activity check this answer. Actually you would have to create service for this: link
Create a transparent activity. Launch the toast while the activity is active and immediately finish the activity.

my android service cannot start after booting device

I bet it's repeated question but I need to ask it again. Service cannot start even I've put following code
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyBroadcastreceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name="com.im.HomeActivity"
android:clearTaskOnLaunch="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.im.ListActivity"
android:label="#string/title_activity_list" >
</activity>
<service
android:name="com.im.SyncService"
android:process=":remote" >
</service>
</application>
and
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, SyncService.class);
context.startService(intent);
Log.i("Autostart", "started");
}
}
Help me, please.
Is your BraodcastReciever getting invoked?
if not then the reason could be following:
Starting with 3.1 when applications are installed they are in a
“stopped” state so they will not be able to run until the user
explicitly launches them. Pressing Force Stop will return them to this
state.
once the user runs the app for the first time (and does not Force Stop
it), everything behaves as before — a reboot will cause BOOT_COMPLETED
broadcasts to be received and so on. However, if the user installs the
app, until and unless they run the app manually, no broadcasts will be
received.
So in your case you will have to create launcher activity and make sure you start that launcher activity at least once then you will start receive boot event broadcast.
Source
Starting from Android 3.1, a user must start the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.
Also android:allowBackup="true" is set in your manifest file, make sure the App is not installed on the SD card. If you are saving to external storage, you will need to setandroid.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE instead.
On some phones(like HTC) there is a Fast Boot option, If it is activated, The BOOT_COMPLETE will not be invoked.
Another approach would be to use Intent.ACTION_SCREEN_ON and check if service is running, if it isn't, then start the service. More info available here

Categories

Resources