Domanda

My application contains a broadcast receiver class only. I just want to display a message in the logcat when the phone is fully booted. For this I used "ACTION_BOOT_COMPLETED".

BroadCast receiver class

package com.example.serviceproject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

    public class BroadCastClass extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            if(intent.getAction() != null)
            {
                if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
                {
                    Log.i("BroadCastClass","BOOT COMPLETED detected");
                }
            }
        }
    };

In the manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.serviceproject"
    android:versionCode="1"
    android:versionName="1.0" 
    android:installLocation="internalOnly"
      android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".BroadCastClass"  android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                    <category android:name="android.intent.category.DEFAULT"/> 
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

But when do I boot my phone the message is not displayed in the logcat. i have something wrong ??

È stato utile?

Soluzione

try with this code...

 package com.example.serviceproject;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;

        public class BroadCastClass extends BroadcastReceiver
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {

                        Log.i("BroadCastClass","BOOT COMPLETED detected");

            }
        };

and manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.serviceproject"
    android:versionCode="1"
    android:versionName="1.0" 
    android:installLocation="internalOnly"
      android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="BroadCastClass">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED">
                    <category android:name="android.intent.category.DEFAULT"/> 
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Altri suggerimenti

Try this :

<receiver android:name="com.example.serviceproject.BroadCastClass" 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>            

Hope this helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top