2013-05-14 15 views
0

我正在創建一個應用程序,我需要使用廣播接收器,我的問題是,我無法將Broadcast.this作爲數據庫上下文傳遞。Android中的上下文封裝openOrclosedatbase異常?

我試圖創建另一個活動,並在數據庫上下文中使用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(); 
    } 
} 
} 
} 

回答

1

嘗試context.getApplicationContext(),也是你應該做它新的線程。

+0

我正在使用單個應用程序,我需要在廣播接收器中使用sqlite數據庫的上下文。 – Shweta 2013-05-14 08:33:50

+0

@shiva然後它只是context.getApplicationContext(); – 2013-05-14 11:24:37

+0

嗨,謝謝你,是的,它現在正在工作... – Shweta 2013-05-15 06:31:40