ブロードキャスト レシーバーを使用する必要があるアプリケーションを作成しています。私の問題は、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();
}
}
}
}