0

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).

  1. logcat filtered for "boot_broadcast_poc" is blank.
  2. logcat filtered for "bootbroadcastpoc" is blank.
  3. I definitely don't see the service started as I don't see any Toast on the screen on boot.
Nirav Zaveri
  • 687
  • 1
  • 9
  • 28
  • Have you run your activity before doing these tests? – CommonsWare May 08 '15 at 12:42
  • I hoped you would come to rescue Mark. Yes, I did run the activity. I even mentioned the same: "I have already launched my app once before testing..." – Nirav Zaveri May 08 '15 at 12:44
  • I wasn't completely sure what "launched my app" was with respect to the activity. Try getting rid of `android:permission="android.permission.RECEIVE_BOOT_COMPLETED"` from the `` element. That shouldn't be there anyway, and it may be interfering with matters. – CommonsWare May 08 '15 at 12:46
  • Well, I have been through your blogs and the issues you have filed with Google. I therefore, obviously, ran the activity on the phone, before rebooting it. Still no good. :( – Nirav Zaveri May 08 '15 at 12:48
  • And yeah, I removed the android:permission code as you mentioned, but no difference in the execution behavior. :-/ – Nirav Zaveri May 08 '15 at 12:57
  • I had a discussion some weeks ago about the same problem, so delete it or make an extra Receiver espacially for this action type. What I think is, Your QUICKBOOT_POWERON attribute is what is causing this problem. Only a few devices have this action type, maybe this is disturbing. Also, replace your category type DEFAULT to LAUNCHER. Maybe it helps.... – Opiatefuchs May 08 '15 at 13:07
  • QUICKBOOT_POWERON was added to resolve the issue. It wasn't there initially. I'll try DEFAULT -> LAUNCHER and let you know. – Nirav Zaveri May 08 '15 at 13:09
  • ok, and don´t forget to do what CommonsWare said. You should update Your post after You have tried with current manifest code# – Opiatefuchs May 08 '15 at 13:11
  • DEFAULT -> LAUNCHER didn't work. Changed it back. Post updated with new Manifest. – Nirav Zaveri May 08 '15 at 13:22
  • @NiravZaveri On Which device you are facing this issue. Is it on some specific device? Please confirm – Krishnakant Dalal May 09 '15 at 14:14
  • I am currently using a Redmi 1s for testing purposes. – Nirav Zaveri May 11 '15 at 10:17
  • I incremented this back up to 0 to remove someone's knee-jerk downvote before the inevitable pile-on's show up. This isn't an unclear question, it's just a *complex* one. –  May 15 '15 at 12:26
  • BTW, having class names that are not capitalized will cause trouble with other folks reading your code sooner or later. Particularly this sort of thing: `class service extends Service` will throw people for a loop, because when they see both Service and service elsewhere, they'll be expecting the former to be a class and the latter to be an instance of that same class. –  May 15 '15 at 12:28
  • Thanks tgm. :) I will update the post shortly. – Nirav Zaveri May 15 '15 at 12:39
  • It works fine without any issue when used with a "static BroadcastReceiver class". I tried with suggestions from all of the answers but none of them worked, at last I find this perfectly working solution. Good luck. – mifthi Mar 08 '17 at 09:16

1 Answers1

3

As stated in comments, I was testing on a Redmi 1s, which is a Xiaomi phone, running MIUI. I tested this same piece code on an emulator (Bluestacks to be precise) and it worked like a charm. This is probably why nobody had a solution! There's no problem in this code!

Turns out, as mentioned here, that the MIUI (my Android version is 4.3), needs another set of permission which is:

<action android:name="android.intent.action.REBOOT"/>

in the Manifest.

Secondly, MIUI seems to have set special permissions to auto-start the app - which can be set in here:

"Settings > Apps > YOUR_APP > Manage permissions"

There you need to enable "Autostart" option.

Hope it helps someone using Xiaomi's Redmi / Mi devices. P.S. My test device was running MIUI-JHCMIBH45.0 when I encountered this issue.

Community
  • 1
  • 1
Nirav Zaveri
  • 687
  • 1
  • 9
  • 28
  • I have searched plenty of docs until I found this.. Thanks very much! I've tested in Xiaomi too. – LiCheng Jan 13 '17 at 15:41