我创建了通过短信发送Proximity Alert
的应用程序。短信送达罚款。但当我的手机屏幕被锁定时,Proximity Alert无法发出警报。我不希望我的手机屏幕一直醒着,我仍然希望我的应用程序即使屏幕被锁定或睡眠也能提醒。任何解决方案。
这是我在主要活动的Proximity Alert
onCreate()
的代码
try{
databaseHelper = new DatabaseHelper(this);
db = databaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select rowid _id,*from latLngTable", null);
go=cursor.moveToFirst();
while (cursor.isAfterLast()!=true){
String strLat = cursor.getString(cursor.getColumnIndex("latitude"));
String strLng = cursor.getString(cursor.getColumnIndex("longitude"));
String mobileNo=cursor.getString(cursor.getColumnIndex("Phone_NO"));
double dLat = Double.parseDouble(strLat);
double dLng = Double.parseDouble(strLng);
float radius = 2000;
LocationManager locationManager2 = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager2.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
String proximitys = "com.realtech.latlngrecorder" + n;
Intent i = new Intent(proximitys);
i.putExtra("phone", mobileNo);
sendBroadcast(i);
PendingIntent pi = PendingIntent.getBroadcast(this, n, i, PendingIntent.FLAG_CANCEL_CURRENT);
locationManager2.addProximityAlert(dLat, dLng, radius, -1, pi);
IntentFilter filter = new IntentFilter(proximitys);
registerReceiver(new ProximityIntentReceiver(), filter);
Log.w("Alert Added", "Alert Added to All Locations");
Toast.makeText(MainPage.this,"Proximity Alert Added",Toast.LENGTH_SHORT).show();
n++;
cursor.moveToNext();
}
}catch (SQLiteException e){
e.printStackTrace();
Toast.makeText(MainPage.this,"Failed to add Proximity Alert",Toast.LENGTH_SHORT).show();
}
我的BroadcastReceiver
接近警报是
public void onReceive(Context context, Intent intent) {
smsNo=intent.getStringExtra("phone");
String proximityKey= LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering=intent.getBooleanExtra(proximityKey,false);
if (entering){
Log.d(getClass().getSimpleName(), "entering");
try {
SmsManager smsManager=SmsManager.getDefault();
String[] numbers=smsNo.split(",");
for (String number:numbers) {
smsManager.sendTextMessage(number, null, "School Van will reach your place in 2 mins.\n\u0bb8\u0bcd\u0b95\u0bc2\u0bb2\u0bcd \u0bb5\u0bc7\u0ba9\u0bcd \u0bb5\u0bb0\u0baa\u0bcd\u0baa\u0bcb\u0b95\u0bbf\u0bb1\u0ba4\u0bc1.", null, null);
}
}catch (Exception e) {
Toast.makeText(context, "No Mobile Number", Toast.LENGTH_SHORT).show();
}
}else {
Log.d(getClass().getSimpleName(), "exiting");
Toast.makeText(context,"exiting",Toast.LENGTH_SHORT).show();
}
}
另一件事,如果我重新启动应用程序,事件再次触发。 即使用户关闭它,也请帮我在后台运行我的应用程序。谢谢。