我设计了一个服务,应该只在一天内调用一次,我从我的DashboardActivity调用它。如果今天的日期与数据库中的任何日期匹配,该服务会向我发送特定记录的通知,但问题是,一旦我访问DashboardActivity,通知就会被解雇我只想让我的通知出现一天只有一次。 这是我的DashboardActivity:
public class DashboardActivity extends AppCompatActivity {
Intent intent;
private Toolbar toolbar;
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_layout);
initializeDashboardActivityViews();
runBackgroundServiceForDeliveryNotification();
if (isMyServiceRunning(MyAlarmService.class)) {
Log.e("NotificationService", "Already Running");
} else {
Log.e("NotificationService", "started Again...");
runBackgroundServiceForDeliveryNotification();
}
}
@Override
protected void onRestart() {
super.onRestart();
initializeDashboardActivityViews();
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private void initializeDashboardActivityViews() {
toolbar = (Toolbar) findViewById(R.id.dashboard_layout_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view_dashboard);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new DashboardAdapter(DashboardActivity.this, "fertigo_pro_regular.ttf");
recyclerView.setAdapter(adapter);
}
private void runBackgroundServiceForDeliveryNotification() {
Log.v("TAG", "Call To RunbackgroundService");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.DAY_OF_MONTH, 13);
calendar.set(Calendar.HOUR_OF_DAY, 06);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
calendar.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(DashboardActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(DashboardActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY , pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
} MyReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("TAG","Call To MyReceiver");
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
} MyAlarmService
public class MyAlarmService extends Service {
DbOperation dbOperation;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
//上下文上下文;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.v("TAG","Call To OnBind service");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.v("TAG","Call To OnCreate service");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent,int flags, int startId) {
Calendar calendar = Calendar.getInstance();
Log.v("BackGround Service", "Start myalaramSevice");
int day = calendar.get(java.util.Calendar.DAY_OF_MONTH);
int month = calendar.get(java.util.Calendar.MONTH)+1;
int year = calendar.get(java.util.Calendar.YEAR);
dbOperation = new DbOperation(this);
sqLiteDatabase = dbOperation.getReadableDatabase();
cursor = dbOperation.getAdvanceBookingDeliveryDates(sqLiteDatabase);
if (cursor.moveToFirst())
{
do{
String getDate;
getDate = cursor.getString(0);
String[] parts = getDate.split("-");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
if (day == Integer.parseInt(part1) && month == Integer.parseInt(part2) && year == Integer.parseInt(part3))
{
Log.e("DATE MATCH","DTAE is match");
displayDeliveryNotification();
}
else{
Log.e("DATE MATCH","DTAE is not match");
}
}while (cursor.moveToNext());
}
else
{
Log.e("MyAlarmService","No notification");
}
Log.v("BackGround Service", "Start myalaramSevice");
return super.onStartCommand(intent, flags, startId);
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
private void displayDeliveryNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
//.setSmallIcon(android.R.drawable.stat_notify_chat)
.setSmallIcon(R.drawable.bricks)
.setContentTitle("Delivery Notification!")
.setContentText("We have some pending Delivery(s) today.")
.setAutoCancel(true);
Intent notificationIntent = new Intent(this, DeliveryNotificationActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(DeliveryNotificationActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
//AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
}
}
答案 0 :(得分:0)
你的
Intent myIntent = new Intent(DashboardActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(DashboardActivity.this, 0, myIntent, 0);
始终从runBackgroundServiceForDeliveryNotification()
开始。