0
我設計了一個應該只在一天中被調用一次的服務,我從我的DashboardActivity調用它。如果今天的日期與數據庫中的任何日期相匹配,該服務會向我發送特定記錄的通知,但問題是一旦我訪問DashboardActivity,通知就會被解僱,我只希望我的通知只出現一次天。 這裏是我的DashboardActivity:Android通知出現在一遍又一遍,而不是隻出現一次
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());
}
}
我在哪裏寫這個意圖? –
你每次都需要被解僱嗎? –
我希望我的通知只能在一天內顯示一次。 –