アクティビティとサービスがあり、サービスからアクティビティにあるメソッドを呼び出したいです。
メソッド ( deleteItem
) は Notification Service を使用して一部の通知をキャンセルしますが、そのため、次のエラーが発生します。
E/AndroidRuntime(13172): java.lang.IllegalStateException: System services not available to Activities before onCreate()
主な活動:
public boolean deleteItem(SQLiteDatabase db, long id) {
if(db.delete(DbHelper.TABLE_NAME, "`_id` = " + id, null) == 1) {
Log.i(TAG, "Item #"+id + " was deleted successfully!");
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel((int) id);
return true;
}
return false;
}
サービス:
@Override
protected void onHandleIntent(Intent intent) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
id = intent.getIntExtra("id", -1);
MainActivity main = new MainActivity();
main.deleteItem(db, id, false);
}
どうすればエラーを解決できますか?