在android中,我想在创建活动时启动服务。
我收到此错误:
E/AndroidRuntime(1433): Caused by:
java.lang.IllegalAccessException: access to class not allowed
我正在使用以下代码:
服务:
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);
}
}
活动:
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);
}
}
机器人清单:
<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>