Android中的上下文包装openOrclosedatbase异常?

Android中的上下文包装openOrclosedatbase异常?,android,android-sqlite,Android,Android Sqlite,我正在创建一个应用程序,其中我需要使用广播接收器,我的问题是,我无法将broadcast.this作为数据库上下文传递 我试图创建另一个活动,并在数据库上下文中使用了activity.this,但没有使用 请任何人告诉我如何在广播接收机中使用数据库上下文 我的广播接收器如下所示: public class BroadCastReceiver extends BroadcastReceiver { String[] Doc_Id; FolderList DocListId; DMS_Databa

我正在创建一个应用程序,其中我需要使用广播接收器,我的问题是,我无法将broadcast.this作为数据库上下文传递

我试图创建另一个活动,并在数据库上下文中使用了activity.this,但没有使用

请任何人告诉我如何在广播接收机中使用数据库上下文

我的广播接收器如下所示:

 public class BroadCastReceiver extends BroadcastReceiver
{
String[] Doc_Id;
FolderList DocListId;
DMS_Database database;
Context context;
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) 
{   
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Toast.makeText(context, " Alarm Received !!! ", Toast.LENGTH_LONG).show();
    database = new DMS_Database(context);
    List<FolderList> DocList = database.getAllDoc();
    database.close();
    System.out.println(DocList.size());
    for(int i=0;i<DocList.size();i++)
    {
    DocListId = DocList.get(i);
    Doc_Id = new String[DocList.size()];
    Doc_Id[0] = DocListId.getId();
    Intent Idintent = new Intent();
    Idintent.putExtra("doc_id", Doc_Id[0]);
    System.out.println(Doc_Id[0]);
    Update up = new Update();
    up.Updatefile(Doc_Id[0]);
    }
}
public void CancelAlarm(Context context)
{
    Intent intent = new Intent(context, BroadCastReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}
public void onLogin(Context context){

}
public void SaveAlarm(Context context)
{
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, BroadCastReceiver.class);
//  intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            1000 * 10, recurringDownload);  
}
 //Tried to make another Activity in same Broadcast receiver, to use database context but o use
public class Update extends Activity
{   
    String ID;
    String fileLongName;
    String UserFileName;
    String url;
    String fileExtension;
    String lastModifiedDate;
    String SubjectType;
    boolean IsUpdated;
    DMS_Database db;
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.5/InterLogicsMobile/InterLogics.asmx";
    private static final String UPDATE_FILE_METHOD = "GetDocumentUpdatedInfo";
    private static final String SOAP_ACTION_UPDATE_FILE = "http://tempuri.org/GetDocumentUpdatedInfo";

public void Updatefile(String Doc_Id)
{
    try
    {
        SoapObject Subfolderrequest = new SoapObject(NAMESPACE, UPDATE_FILE_METHOD);
        Subfolderrequest.addProperty("DocumentID", Doc_Id);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Subfolderrequest);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION_UPDATE_FILE , envelope);
        SoapObject DocResponse = (SoapObject)envelope.getResponse();    
        Log.i("Uspdated Documents", DocResponse.toString());    
        for(int i=0; i < DocResponse.getPropertyCount(); i++)
        {
            SoapObject SingleSubFolder = (SoapObject)DocResponse.getProperty(i);        
            ID = SingleSubFolder.getProperty(0).toString();
            fileLongName = SingleSubFolder.getProperty(1).toString();
            UserFileName = SingleSubFolder.getProperty(2).toString();
            url = SingleSubFolder.getProperty(3).toString();
            fileExtension = SingleSubFolder.getProperty(4).toString();
            lastModifiedDate = SingleSubFolder.getProperty(5).toString();
            SubjectType = SingleSubFolder.getProperty(6).toString();
            IsUpdated = SingleSubFolder.hasProperty("IsUpdated");
            if(IsUpdated==true){
            db = new DMS_Database(Update.this);
            db.update_Doc(ID, UserFileName, url);  
 //Gettig error here & in database update_Doc method...
            db.close();
    }
}   
    catch(Exception e) 
    {
        e.printStackTrace();
        Toast.makeText(this, " Network Exception : " + e
                + "Please check network connectivity.", Toast.LENGTH_LONG).show();
    }
}
}
}
公共类BroadCastReceiver扩展了BroadCastReceiver
{
字符串[]文档Id;
FolderList DocListId;
DMS_数据库;
语境;
最终公共静态字符串ONE_TIME=“onetime”;
@凌驾
公共void onReceive(上下文、意图)
{   
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
Toast.makeText(上下文,“收到报警!!!”,Toast.LENGTH\u LONG.show();
数据库=新的DMS_数据库(上下文);
List DocList=database.getAllDoc();
close()数据库;
System.out.println(DocList.size());

对于(int i=0;iTry,您也应该在新线程上执行此操作。

我使用的是单个应用程序,我需要在广播接收器中为sqlite数据库使用上下文。@shiva然后它只是context.getApplicationContext();