我制作了一个应用程序,可以在Android手机的下拉状态栏中设置通知.但是,我的代码中存在一个错误(有时会设置通知,有时则不会).我希望能够检查(在代码中)如果通知对用户是可见的.(即用户可以在状态栏中看到通知吗?).
我怎样才能做到这一点?(提前致谢).
非常感谢示例代码.
我有一个带有列标题的表格:| _id(长)| 名称(字符串)| x(整数)| y(整数)|
我想删除表名为myName的行.
// In onCreate
dbHelper = new DBAdapter(this);
dbHelper.open()
// Function in DBAdapter class
public boolean deleteTitleGivenName(String myName)
{
return dbHelper.delete(DATABASE_TABLE_2, KEY_NAME + "=" + myName, null) > 0;
}
// Function call in java code
dbHelper.deleteTitleGivenName(myName); // this is where my code fails
dbHelper.close();
Run Code Online (Sandbox Code Playgroud)
就像一个注释:myName肯定在数据库中.此外,我无法使用ROWID,因为我从ListView获取myName.
我刚开始用Android编程,我已经尝试了几天来解决这个问题.我的WHERE子句是否正确(KEY_NAME +"="+ myName)?
提前致谢.
我正在创建一个应用程序,用户可以根据GPS位置设置警报.我只想在任何时候激活1个警报.因此,当用户设置第二个警报时,我想取消第一个警报的通知(然后为第二个警报设置新的通知).
现在,我的通知继续堆叠(因为我无法删除它们,所以它们都是活动的).这是我的代码,我试图删除警报和通知:
// Stop the location alarm activity
Intent intentAlarmService_delete = new Intent(v.getContext(), AlarmService.class);
stopService(intentAlarmService_delete); // I think this calls onDestroy() in AlarmService class ...
mNtf = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNtf.cancelAll();
Intent alarmIntent2 = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1,
alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
Run Code Online (Sandbox Code Playgroud)
这是我的AlarmService.class中的onDestroy()函数(我不确定何时调用它...)
public void onDestroy(){
super.onDestroy();
mNtf.cancel(NOTIFICATION_ID1);
Intent alarmIntent = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1,
alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class);
stopService(intentAlarmService);
mNtf.cancel(NOTIFICATION_ID1);
mNtf.cancelAll();
}
Run Code Online (Sandbox Code Playgroud)
然后,这就是我设置新警报和通知的方式:
Intent intentAlarmService2 = new Intent(v.getContext(), …Run Code Online (Sandbox Code Playgroud) 我仍在研究基于位置的警报android应用程序.我有一个AlarmService类,可以启动通知和接近警报.我正在开始这项服务:
startService(intentAlarmService);
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下命令停止服务:
Intent intentAlarmService = new Intent(this, AlarmService.class);
stopService(intentAlarmService);
Run Code Online (Sandbox Code Playgroud)
这就是:服务停止,但是当我启动另一个服务实例(即退出应用程序,启动应用程序,启动服务)时 - 我发现(通过Toasts)服务的先前实例仍然是运行.例如,在AlarmService类中,有一个带有onLocationChanged方法的LocationListener.所以,在这个方法中,我把:
Toast.makeText(AlarmService.this, "AlarmTitle: " + mAlarmTitle, Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
当我重新启动服务时,Toasts会继续显示之前的AlarmTitles和当前的AlarmTitle.
因此,当我尝试停止AlarmService时,某些功能无效 - 这可能是什么?
注意:当我重新安装应用程序时,服务将停止实时.然后当我启动服务时,只有Toast中显示当前的AlarmTitle(我希望每次都这样).
我的服务有问题.任何想法我能做什么?
谢谢.
我的APP代码:
public void onDestroy() {
super.onDestroy();
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class);
stopService(intentAlarmService);
mNtf.cancel(NOTIFICATION_ID1);
mNtf.cancelAll();
}
Run Code Online (Sandbox Code Playgroud) android ×4