1

I want to use services in android.I worte code as a simple

That's my mainactivity class;

public class MainActivity extends Activity 

{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public class MReceiver extends BroadcastReceiver 
{        

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent(context, MService.class);
         context.startService(myIntent);
    } 
}

public class MService extends Service
{

    @Override
    public IBinder onBind(Intent intent) 
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        Log.i("onCreate", "Service onCreate");
    }

}

}

and that's my xml file;

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

        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />

        <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" >
            <activity
                android:name="com.example.service.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <service android:enabled="true" android:name=".MService" />

             <receiver android:name=".MReceiver" android:enabled="true" android:exported="false">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>

        </application>

    </manifest>

But when I open my tablet after close that program is crashing and there

error is ;

"error opening trace file" "unable to instantiate receiver com.example.service.MReceiver"

What must I do ? Please help me.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
user3041263
  • 53
  • 1
  • 1
  • 8

2 Answers2

0

You can declare your Receiver class as static class in Activity...

public static class MReceiver extends BroadcastReceiver {        

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, MService.class);
     context.startService(myIntent);
} 
}

or write Receiver class in separate java file...

if you write it in MainActivity, write it in maifest like

 <receiver name=".MainActivity$MReceiver" ....>

or if it is a separate file, declare like

  <receiver name=".MReceiver" ...>

(here make sure that your MReceiver class package name and application package name are same)

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
-1

In your manifest you have declared your <receiver> with this:

android:exported="false"

That is wrong. When you do that you are making your receiver private, so the Android framework cannot instantiate it when it wants to call your onReceive() during the BOOT_COMPLETED broadcast.

Just remove android:exported="false" from the <receiver> tag and you should be OK.

David Wasser
  • 93,459
  • 16
  • 209
  • 274