Android: Start service at boot, but do not launch GUI - android

I'm writing an Android app that has two major components: a service which starts up at boot time, and a GUI that I want to start only when I manually launch it via its icon, not when the device boots. I know how to start the service at boot time, but it also launches the GUI at boot time, which I don't want.
I presume that this has something to do with the settings in my manifest, but despite trying a number of things, I haven't figured out how to prevent the GUI from also starting at boot time.
I should add that I do not programmatically launch the GUI at boot time. I do reference static public variables within the GUI's activity class, but I do not make any method calls or send any intents to the GUI's activity.
Here is my manifest. What am I doing wrong? Thank you very much.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package.name"
android:versionCode="0"
android:versionName="0.1.0">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon"
android:label="#string/app_name"
android:allowBackup="true" >
<!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I want MainActivity to only start when I
select its icon, NOT at boot time. However,
it always starts up at boot.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
<activity
android:name=".MainActivity"
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>
<!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
MyBootReceiver properly starts up at boot time,
and it properly invokes MyBootService. At
the appropriate time, MyBootService invokes
RegisterActivity.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-->
<activity android:name=".RegisterActivity"
android:label="#string/app_name">
</activity>
<receiver
android:name=".MyBootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="my.package.name" />
</intent-filter>
</receiver>
<service android:name=".MyBootService" />
</application>
</manifest>
Adding broadcast receiver class:
package my.package.name;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent bootIntent = new Intent(context, MyBootService.class);
bootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(bootIntent);
}
}
Adding service class ...
package my.package.name;
import java.util.ArrayList;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.TelephonyManager;
public class MyBootService extends Service {
private static final String GOOGLE_ACCOUNT_TYPE = "com.google";
private static final String GOOGLE_ACCOUNT_FEATURE = "service_ah";
private Context context = null;
#Override
public IBinder onBind(Intent intent) {
this.display("onBind");
return (null);
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
AccountManager am = AccountManager.get(this);
am.getAccountsByTypeAndFeatures(GOOGLE_ACCOUNT_TYPE, new String[]{GOOGLE_ACCOUNT_FEATURE},
new AccountManagerCallback<Account[]>() {
#Override
public void run(AccountManagerFuture<Account[]> acclist) {
MyBootService parent = MyBootService.this;
Intent regIntent = new Intent(parent.getApplicationContext(), RegisterActivity.class);
try {
ArrayList<String> accountNameList = new ArrayList<String>();
for (Account a: acclist.getResult()) {
accountNameList.add(a.name);
}
regIntent.putStringArrayListExtra("accountNames", accountNameList);
try {
TelephonyManager tmgr = (TelephonyManager) parent.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNo = tmgr.getLine1Number();
regIntent.putExtra("phoneNumber", phoneNo);
}
catch (Throwable t) {
}
}
catch (Throwable t) {
// put error message here
}
regIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
parent.startActivity(regIntent);
}
}, null);
return (START_STICKY);
}
#Override
public void onDestroy() {
this.display("onDestroy");
super.onDestroy();
}
}
MORE INFO
I figured out some of what is going on. First of all, I was mistaken in saying that my MainActivity was starting. Upon more detailed debugging, I see that its onCreate() and onResume() methods were not being called. However, the app's view was showing up: a black screen with the name of the app and the default icon. I originally mistook that for an indication of a full startup.
This, of course, raises the question of why that view showed up on boot, in the first place. I have some info about this, although I'm still confused as to what is going on. I stripped down the onCreate() method of the RegisterActivity class that gets invoked by MyBootService. When this.getIntent() is called, the application's view shows up on boot. When this.getIntent() is commented out, the application's view does not show up on boot.
Note that this is onCreate() of the RegisterActivity class, NOT of MainActivity.
Do any of you know what could be causing the application's view to show up when this.getIntent() is called?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the following line is commented out, the
// application's view does not show up on boot;
// if the following line is not commented out,
// the application's view shows up.
Intent intent = this.getIntent();
}

I think what you are seeing is the preview used by Android to make the app startup seems faster. This preview is made based on the theme of your activity.
Try setting a custom theme for your RegisterActivity which more closely looks like the final result. For example, if your activity is a dialog, create a theme extending Theme.Dialog or Theme.Light.Dialog
You can get more information on this blog post from Cyril Mottier: Android App Launching Made Gorgeous
EDIT: changed to actually answer the question

THE REAL, ACTUAL ANSWER
Because of the help and feedback I got here, I finally figured out my problem and solved it:
I was wrong in using an Activity in the first place (my RegisterActivity class), because I don't want the functionality that I originally put into RegisterActivity to be associated with a GUI. Due to inexperience with the Android model and lack of sufficient documentation reading, I didn't realize Activity always has a view associated with it.
Now that I know this, I refactored my app so that all the functionality formerly in RegisterActivity has been moved to MyBootService. Now, no view pops up on boot, and my app does what I want.
Thanks to all for your help and patience.

Related

Checking if a Broadcast Receiver is Working

I am working on an app that will use a BroadcastReceiver to pick up certain SMS messages that meet certain criteria. It's a long way short of finished, mainly because it seems that the BroadcastReceiver isn't working. I've tried to use a toast to check if it's working but I get no result. So either:
The BroadcastReceiver is not working
My method of testing is wrong
Or both
The AndroidManifest.xml file is
<?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="com.example.alert6">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<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/Theme.Alert6">
<activity
android:name=".SendResponseActivity"
android:parentActivityName=".ReceiveAlertActivity">
</activity>
<activity
android:name=".ReceiveAlertActivity"
android:parentActivityName=".MainActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SmsBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
And this is the SmsBroadcastReceiver java file
package com.example.alert6;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"SMS Is Being Received",Toast.LENGTH_LONG).show();
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
String smsSender = "";
String smsBody = "";
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsSender = smsMessage.getOriginatingAddress();
smsBody = smsMessage.getMessageBody();
}
if (smsSender.equals("+420775367297")) {
if (smsBody.contains("Test")) {
// I haven't done this bit yet
}
}
}
}
}
When I send a SMS to the test device I would expect a toast message saying "SMS Is Being Received". Instead the app disappears from the screen and my default SMS app appears instead. What am I doing wrong?
Since Android API 23 you need set permission not only in manifest class, but also set permisson manually. You should set it in app's settings, or you should make permission reqeust from your code. This is what you need add in your main activity's file:
// constant for request code
private final int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10; // any number which you want
//function for permission request
#RequiresApi(api = Build.VERSION_CODES.M)
public void checkPermission() {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED)) {
requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS}, MY_PERMISSIONS_REQUEST_SMS_RECEIVE);
}
}
Also you need call this function somewhere. For example you can make it in onCreate().
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkPermission();
}
//do something else
}
After a huge amount of fiddling and testing, I came to the conclusion that the broadcast receiver is not working. Probably due to Android's limitations on "dangerous" permissions. I don't think this thread has any life left in it, but I will continue to try to persuade my Android app to work with the necessary permissions.

Do something when a headset is plugged in when my app is running in the background. (if possible i want to do it with broadcast receivers)

I want to do something when a headset is plugged in when my app is running in the background. (if possible I want to do it with a broadcast receiver)
I tried the code below:
--ReceiveBroadcast--
package com.example.openmusiconheadsetconnect;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class ReceiveBroadcast extends BroadcastReceiver {
public ReceiveBroadcast() {
}
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Received!",Toast.LENGTH_LONG).show();
}
}
--Manifest--
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.openmusiconheadsetconnect" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".ReceiveBroadcast"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
Thank you!
Your code is correct, but as far as I know, you cannot put the HEADSET_PLUG filter on the manifest.
Instead, create a receiver in its own class, and make it listen for USER_PRESENT (screen unlocked) or BOOT_COMPLETED in the manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<receiver android:name="classes.myReceiver" >
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
When triggered by such events, your receiver should start the service:
public class myReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
Intent service = new Intent(ctx, VoiceLaunchService.class);
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
ctx.startService(service);
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
ctx.stopService(service);
}
}
The service will now register the receiver that will be listening to the HEADSET_PLUG intent, in its onCreate method:
#Override
public void onCreate() {
super.onCreate();
speechReconRx=new SpeechReconControlReceiver(this);//"this" will allow you to call service's methods from the receiver
registerReceiver(speechReconRx, new IntentFilter(Intent.HEADSET_PLUG));
}
It's is a hassle, but you'll need it if you don't want to use an activity.
It is google's fault for not letting us put PLUG receivers in the manifest! Finally make the Broadcast that will take action when the headset is plugged in.
public class SpeechReconControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
Log.e("joshcsr","HEADSET PLUGGED!");
if(intent.getStringExtra("command")!=null){
c=intent.getStringExtra("command");
}
//run some methods from the service
if (c.equals("resume")) {
sService.resume();
}
if (c.equals("pause")) {
sService.pause();
}
if (c.equals("stop")) {
sService.stop();
}
}
}
To wrap, up you will need:
*A receiver for the BOOT/Screen unlock events.
*A Service to hold everything that will run on the background and to register your headset listening broadcast.
*And a receiver for the headset Plug, that will take action and call methods hosted in the service.
I've did this yesterday, and it works from Jelly bean to Lollipop ...and perhaps even older versions. Cheers.
First you'll need permission to start app in background after boot is completed.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and also specify this in your broadcast receiver,
<receiver android:name=".YourBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Then create a service that run your application in background, and inside the service use AudioManager.isWiredHeadsetOn() to check if the headset is plugged in. And if so, do the task you want.
while(AudioManager.isWiredHeadsetOn()){
//your task goes here
}
Also add the permission in manifest
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

My custom system app doesn't toggle FlightMode as it should

My girlfriend has a problem with her Samsung Galaxy S with Cyanogenmod 10.2. Every time she restarts her phone, the APN list is empty and she can't add new (which means she can't use mobile internet nor send/receive MMS). To temporarily fix it until next reboot, she needs to turn airplane mode on, wait few seconds, and then turn airplane mode off again.
To ease things up, I've written a simple application which is supposed to do it instead of her. But it doesn't work - I see in logs that the app was started, but it doesn't toggle Airplane mode.
Permissions should be set correctly, I even installed the app into /system/app to make Android think it's a system app. The permissions are displayed correctly when I look at the application in Applications list.
Here's my code:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.berzeger.autoflightmode"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".AutoFlightModeService" android:label="AutoFlightMode Service" />
<receiver
android:name=".ServiceReceiver"
android:enabled="true"
android:exported="true"
android:label="ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
ServiceReceiver.java
package cz.berzeger.autoflightmode;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ServiceReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, AutoFlightModeService.class);
context.startService(serviceIntent);
}
}
}
AutoFlightModeService.java:
package cz.berzeger.autoflightmode;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.provider.Settings;
public class AutoFlightModeService extends Service {
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Turn AirPlane mode on
Settings.Global.putString(getContentResolver(), "airplane_mode_on", "1");
// All services run in one thread. We need to explicitly create a new thread,
// if we want to implement non-blocking wait.
new Thread( new Runnable() {
public void run() {
try { Thread.sleep( 5000 ); }
catch (InterruptedException ie) {}
// After 5 seconds, turn Airplane mode off
Settings.Global.putString(getContentResolver(), "airplane_mode_on", "0");
}
}).start();
return START_NOT_STICKY;
}
}
Any ideas what I'm doing wrong? Thanks.
adb shell ls system/
addon.d
app
bin
blobs
build.prop
etc
extras
fonts
framework
lib
lost+found
media
***priv-app***
recovery-from-boot.p
tts
usr
vendor
xbin
The app needs to be installed in priv-app to take the system permissions.

Android Boot-Up BroadCast Not invoking

I am currently trying to make a broadcast receiver which will invoke after android device boots and then will run a background service. I have tried many examples but don't know where I'm going wrong. I am following this example:
https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot
I have imported this whole project in my workspace and tried to run. But the receiver didn't invoked or so.
Please help me out.
My Testing Device is: Motorolla Xoom with ICS 4.0.3
EDIT
Manifest
<uses-sdk android:minSdkVersion="8" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.REBOOT" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service
android:name="awais.soft.MyService"
android:enabled="true" >
<intent-filter>
<action android:name="awais.soft.MyService" >
</action>
</intent-filter>
</service>
<receiver android:name="awais.soft.ServicesDemoActivity" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
<category android:name="android.intent.category.HOME" >
</category>
</intent-filter>
</receiver>
</application>
Broadcast Receiver
package awais.soft;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class ServicesDemoActivity extends BroadcastReceiver {
static final int idBut = Menu.FIRST + 1, idIntentID = Menu.FIRST + 2;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("Awais", "onReceive:");
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Intent i = new Intent();
i.setAction("awais.kpsoft.MyService");
context.startService(i);
}
}
}
Service
package awais.soft;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.is);
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
I am something like this in My app and Its Working for me.
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public final void onReceive(final Context context, final Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
// CustomLog.i("Boot Completed");
}
}
}
Android Manifset
<receiver android:name=".model.service.DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.HOME"></category>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.REBOOT" />
Please check if you have given permission for RECEIVE_BOOT_COMPLETED
see i am posting you eample that will help you
For some applications, you will need to have your service up and running when the device is started, without user intervention. Such applications mainly include monitors (telephony, bluetooth, messages, other events).
At least this feature is currently allowed by the exaggeratedly restrictive Android permissions policy.
Step 1: First you'll need to create a simple service, defined in Monitor.java:
public class Monitor extends Service {
private static final String LOG_TAG = "::Monitor";
#Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e(LOG_TAG, "Service started.");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
#Override
public IBinder onBind(Intent intent) {
Log.e(LOG_TAG, "Service bind.");
return null;
}
}
Step 2: Next we need to create a Broadcast receiver class, StartAtBootServiceReceiver.java:
public class StartAtBootServiceReceiver extends BroadcastReceiver
{
private static final String LOG_TAG=StartAtBootServiceReceiver";
#Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("test.package.Monitor");
context.startService(i);
}
}
}
Step 3: Finally, your AndroidManifest.xml file must contain the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.package.Monitor"
android:versionName="1.0"
android:versionCode="100"
android:installLocation="internalOnly">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="8"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="test.package.Monitor">**
<intent-filter>
<action android:name="test.package.Monitor">
</action>
</intent-filter>
</service>
<receiver android:name="test.package.StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
I need to highlight some of the most important aspects, key factors for possible errors in implementation:
1) The permission android.permission.RECEIVE_BOOT_COMPLETED must be provided (in the manifest xml)
2) The installation must be performed in internal storage, not on SDCARD! To enforce this use android:installLocation="internalOnly" in the manifest
Everything was fine..:S
The problem was with device..(i.e. Motorolla Zoom ICS 4.0.3)
Now tested on Galaxy Tab With 2.2 and Working fine..
Thanks all for your time
If your phone is rooted then you will have trouble in Android Boot-Up BroadCast invoking otherwise you have to ensure your app has required root permissions
The problem persists in the case of devices having android version more than 3.0, by the way its not the problem it has been done for security purposes by google i guess..If u have to run the service on boot you have to make a custom intent & broadcast it. For making custom intent you have to make a service file from where u have to broadcast that intent on boot complete & your service file(that u want to run) will receive that intent on its onReceive method & your service will run.One more thing the service file you will create to call your service that you want to run should be kept on system/app folder of file explorer of device, if your file system shows sorry read only file system then from command prompt do just adb remount & then push the file on device,restart your system your service will run..Cheers!!

Service doesn't show Logs

Question
I got a little problem to see what happens in my Service. I don't get any logs of it. So first I thought I probably don't start my Service, but this isn't the problem. When I go to Running-Apps on my Device the Service is listed up there.
Now lets take a look at the Code:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.ivocore"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.IVOCore"
android:debuggable="true">
<service android:name=".service.DataHandlerService" />
<!-- android:enabled="true"
android:icon="#drawable/ic_launcher"/>
android:process=":DataHandler" -->
<activity
android:label="#string/app_name"
android:name=".LoginActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<action android:name="de.ivocore.LOGINCHECK"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"></activity>
</application>
<!-- Definieren welche Permission's die Applikation besitzt -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
MainActivity.class
I start the Service here in my onCreate().
package de.ivocore;
import de.ivocore.service.DataHandlerService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends Activity {
private final String LOAD_ORDER = "de.ivocore.MainActivity.LOAD_ORDER";
//Verschiedene Integer definieren für Switch/Case damit der Service weis was zu tun ist
int FIRST_LOAD = 0; //Wird gesendet beim onCreate der Activity
int RELOAD = 1; //Wird gesendet wenn der User einen reload möchte
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Service starten zusätlich wird folgender Parameter mitgegeben "onstart"
//dies zeigt dem Service das die Activity gerade gestartet wurde.
Intent i = new Intent(this, DataHandlerService.class);
i.putExtra(LOAD_ORDER, FIRST_LOAD);
startService(i);
}
public void onReceive(Intent intent){
}
}
DataHandlerService.class
Here i catch the Intent over onStartCommand(), which probably dosn't work. But I don't get whats wrong there...
public class DataHandlerService extends Service {
private AlarmManager alarmManager;
private Calendar calendar;
private static final int PERIOD = 60000 * 60; // 1h
private final String LOAD_VIDEO = "de.ivocore.service.DataHandlerService.LOAD_VIDEO";
private final String LOAD_ORDER = "de.ivocore.MainActivity.LOAD_ORDER";
LoginAdapter mDbHelper = new LoginAdapter(this);
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onStartCommand(Intent intent, int flags, String startID){
int load = intent.getIntExtra(LOAD_ORDER, 0);
Log.d("DataHandlerService", "Activity startet mit folgendem Parameter ="+load);
switch (load){
case 0:
Log.d("DataHandlerService", "Es wird def OfficeTimerOnStart gestartet");
checkOfficeTimeOnStart();
break;
case 1:
getOrders();
break;
}
}
public void onDestroy(){
stopSelf();
}
}
Please tell me what I'm doing wrong, that I can see my Logs!
Thank you in Advance.
Best Regards
safari
This is quite old post, but I ended here anyway, when I had the same problem in Android Studio. For those who will end up here just like me, following might be the problem:
You might be having problems to use DDMS Logging feature.
Try to get the logs in a file to your filesystem and search for your logs.
1 > Go to command prompt.
2 > Traverse to the platform tools dir of your SDK location
3 > type " adb logcat > D:\log.txt
------
4. After starting your app exit the logcat by pressing ctrl+c
then try to verify the presence of your logs.
Did you select the Log level as debug? If not please do so in Eclipse debug prespective

Categories

Resources