Manish
Manish

Reputation: 19

Can not start my service

In android I want to start a service when an activity is created.

I'm getting this error:

E/AndroidRuntime(1433): Caused by:
    java.lang.IllegalAccessException: access to class not allowed

I'm using the following code:

Service:

class Myservice extends Service 
{
    @Override
    public void onCreate() {
        Log.d("Debug", "on create delete sms");
        super.onCreate();
    }

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

    @Override
    public void onDestroy() {
          super.onDestroy();
          Log.d("Debug", "on destroy delete sms");
        }

    @Override
    public void onStart(Intent intent1, int startId) {

        Log.d("Debug", "on start delete sms");
        super.onStart(intent1, startId);

    }
}

Activity:

public class ServicetestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Intent serviceIntent = new Intent();
        serviceIntent.setAction("org.avd.Myservice");
        getApplicationContext().startService(serviceIntent);
    }
}

androidmanifest:

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ServicetestActivity"
            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:name="org.avd.Myservice">
        <intent-filter>
                        <action android:name="org.avd.Myservice" />
           </intent-filter>
        </service>
    </application>

Upvotes: 1

Views: 392

Answers (3)

coading fever
coading fever

Reputation: 221

Make your service class file as public identifire

then call from your activity by this way

startService(new Intent("yourActivity.this",Myservice .class));

and if you want to stop your service just put down the code

stopService(new Intent("yourActivity.this",Myservice .class));

Upvotes: 3

Jay Thakkar
Jay Thakkar

Reputation: 743

Please Look that your custom class is public, and it has a public zero-argument constructor, also look that the constructor chains to the superclass' constructor.

Upvotes: 0

Vipul
Vipul

Reputation: 28103

Make sure your Service class is public.

Upvotes: 0

Related Questions