0

How to start a service in android using nested class when mobile is switched ON?

I have the package contains nested class.

package Name

com.android

  1. MainActivity
  2. BroadCastReceiver

I am trying to reboot my BroadCast receiver. But it doesn't work (getting Failed). I don't know if the reason is, because of any problems for nested classes.

 <receiver android:name="ConnectionReceiver"></receiver> 

<receiver android:name="com.android.MainActivity .BroadCastReceiver "> 
<intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED" /> 
</intent-filter> 
</receiver> 
r4.
  • 358
  • 1
  • 6
  • 22
Mercy
  • 1,862
  • 9
  • 39
  • 75

2 Answers2

0

manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="onchip.automobile.caraccessory1"
    android:versionCode="1"
    android:versionName="1.0" >    

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver 
            android:name=".Onchip_BroadcastReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>            
        </receiver>

        <service 
            android:name=".Onchip_BackgroundService">
            <intent-filter >
                <action android:name="onchip.automobile.caraccessory1.BACKGROUND_SERVICE" />                
            </intent-filter>            
        </service>
    </application>

</manifest>

Onchip_BroadcastReceiver.java

package onchip.automobile.caraccessory1;

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


public class Onchip_BroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent serviceIntent = new Intent("onchip.automobile.caraccessory1.BACKGROUND_SERVICE");        
        context.startService(serviceIntent);
    }   
}

Onchip_BackgroundService.java

package onchip.automobile.caraccessory1;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;


public class Onchip_BackgroundService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Onchip_BackgroundService Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "Onchip_BackgroundService Started", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Onchip_BackgroundService Destroyed", Toast.LENGTH_LONG).show();
    }      

}
Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
  • ya inside only .i called like this receiver = new ConnectionReceiver(); registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); – Mercy Jan 19 '12 at 09:43
  • please see the example at link given, you will understand. – Yugandhar Babu Jan 19 '12 at 09:45
  • did you used same code or modified ? because i used same code its working for me. – Yugandhar Babu Jan 19 '12 at 09:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6847/discussion-between-leema-rose-and-yugandhar-babu) – Mercy Jan 19 '12 at 10:01
0

R u having BroadcastReceiver as nested class. if yes dont do this.

1)if you have receiver as nested class, register it in activity dynamically, do not register in manifest

2) if you are registering it in manifest , make separate class for Receiver.

AAnkit
  • 27,299
  • 12
  • 60
  • 71