Android Boot-Up BroadCast Not invoking

好久不见. 提交于 2019-12-29 07:21:08

问题


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();
}

}


回答1:


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




回答2:


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




回答3:


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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/9426189/android-boot-up-broadcast-not-invoking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!