First off, please don't mark this as duplicate, I have tried to resolve the issue using several different questions here on SO, and tried each and every solution for this issue, over SO.
Therefore, I reproduce my entire code here along with the logcat.
The Issue:
I am trying to write an application that will boot up on reboot of a device. I can see several applications receiving the BOOT_COMPLETED action in the logcat, but I can't see my application anywhere in logcat on device reboot.
Point to Note:
I have already launched my app once before testing through Device Reboot.
Code Files:
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.abc"
android:versionCode="1"
android:versionName="1.0" >
<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" >
<receiver android:name="com.xyz.abc.autostart" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".hello"
android:label="@string/title_activity_hello" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service"
android:enabled="true" />
</application>
</manifest>
Autostart.java
package com.xyz.abc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by admin on 008, 8 May 2015.
*/
public class autostart extends BroadcastReceiver
{
public void onReceive(Context context, Intent arg1)
{
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, service.class));
}
}
service.java
package com.xyz.abc;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
* Created by admin on 008, 8 May 2015.
*/
public class service extends Service
{
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("bootbroadcastpoc","NotifyingDailyService");
return super.onStartCommand(pIntent, flags, startId);
}
}
hello.java
package com.xyz.abc;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class hello extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_hello, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Finally, the logcat filtered by BOOT_COMPLETED: UPDATE : Removed the logcat as it was not necessary (Also I have found the solution which I will have posted down shortly).
- logcat filtered for "boot_broadcast_poc" is blank.
- logcat filtered for "bootbroadcastpoc" is blank.
- I definitely don't see the service started as I don't see any Toast on the screen on boot.