我正在创建一个应用程序,其中我需要使用广播Receiver,我的问题是,我无法将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();
}
}
}
}