Service doesn't show Logs - android

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

Related

Android KitKat not showing Toast once the App goes to background

I am running a service from my Android test app, which needs to show toast from counter value which is continuously increasing in a service.
Its starting well, and showing the toast. But, once I press back/home button to keep the app in background, the toast stops showing. When I again bring the app to foreground, again the toast started visible.
This problem happens sin Kitkat. But in JellyBeans and below, its working fine.
Here is my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.mysms"
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="19"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name="com.rtrgroup.mysms.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.rtrgroup.mysms.MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
Here is my MainActivity.java file.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
}
Here is my service code, MyService.java:
package com.rtrgroup.mysms;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.*;
import android.widget.Toast;
public class MyService extends Service {
Handler handler;
int count = 0;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
handler.postDelayed(test, 1000);
return START_NOT_STICKY;
}
Runnable test = new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "TOAST count = " + String.valueOf(count), Toast.LENGTH_SHORT).show();
count++;
handler.postDelayed(test, 5000);
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
So, how to make toast visible always, even the app is in background and the service is running? Please explain.
if you want to detect when the user put your app in background , override this method
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
switch (level) {
case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
// do your magic here
break;
}
}
check the documentation http://developer.android.com/reference/android/content/ComponentCallbacks2.html#TRIM_MEMORY_UI_HIDDEN
also you can start your service in "foreground" mode for this check
Show notification from background started service
or you can show a notification instead a toast.

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.

Receiving the Broadcast

I want to make a application that gets the incoming calling number if the call is from a specific number then do some stuff . I want to get the incoming calling number even when the application is not running .. I am using the BraodcastReceiver to get the incoming number .
I have two java class one which extends he activity and the other extends the BraodcastReceiver for getting the incoming calling number .
Main class which extends activity :
package digicare.ringmanager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Main_Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_layout, menu);
return true;
}
}
it is pretty simple and the Number checker class which extends the BroadcastReceiver :
package digicare.ringmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class number_checker extends BroadcastReceiver {
private int ringer_mode ;
private String AM_str;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
AudioManager AM =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
Log.w("start", "starting the Main_Activity");
AM_str=String.valueOf(ringer_mode);
Log.w("Ringer_mode at start", AM_str);
//setting the ringer mode on number match
try {
Bundle extras=intent.getExtras();
if (extras !=null){
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("state at start",state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
String phonenumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//AM.setRingerMode(1);
ringer_mode =AM.getRingerMode();
AM_str=String.valueOf(ringer_mode);
Log.w("Ringer_mode at ringing", AM_str);
Log.w("Number", phonenumber);
if (phonenumber.equals("1234")){
Log.w("yahoo", "Number matched");
if (ringer_mode==AudioManager.RINGER_MODE_SILENT || ringer_mode==AudioManager.RINGER_MODE_VIBRATE ){
AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Log.w("Phone number is matched .!", "Now , ringer mode is normal");
int now_nor =AM.getRingerMode();
String now_nor_str=String.valueOf(now_nor);
Log.w("ring_mode at num matched",now_nor_str);
}
}
}
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK ) || state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
int now_nor =AM.getRingerMode();
String now_nor_str=String.valueOf(now_nor);
Log.w("ring_mode at offHock",now_nor_str);
if (ringer_mode==AudioManager.RINGER_MODE_NORMAL){
AM.setRingerMode(AudioManager.RINGER_MODE_NORMAL );
Log.w("Normal", "");
}else if (ringer_mode==AudioManager.RINGER_MODE_SILENT ){
AM.setRingerMode(AudioManager.RINGER_MODE_SILENT );
Log.w("silent", "");
}else if (ringer_mode==AudioManager.RINGER_MODE_VIBRATE ){
AM.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Log.w("vibrat", "");
}
// Log.w("Again", "Now the Ringer mode is get back ");
int now =AM.getRingerMode();
String now_str=String.valueOf(now);
Log.w("ring_mode at end ",now_str);
}
}
} catch (Exception e) {
// TODO: handle exception
Log.w("MY_DEBUG_TAG", e);
}
}
}
And the AndroidManifist.xml is this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="digicare.ringmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="digicare.ringmanager.Main_Activity"
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="number_checker" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
</manifest>
but this not working this cannot get the incoming calling number .
wot could i do ???? have to call the number_checker class for register the Braodcast ???
please help i am a new android developer
You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.
Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.
also check this link :
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html
NEW Update:
check this link http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61&aaid=86
Update 2
put . before the receiver name. like this <receiver android:name=".number_checker" >

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

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.

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!!

Categories

Resources