在通知按钮单击时从数据库中删除行

时间:2018-03-12 10:55:16

标签: android sql sqlite android-notifications

我是android的新手。我正在制作一个应用程序,它将发送通知,提供两个按钮,如“有用”和“无用”。 如果用户点击“有用”,用户将被重定向到activity.java,如果用户点击“无用”,该记录将从sqlite数据库中删除。代码如下,

通知

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setVibrate(new long[]{0, 100, 100, 100, 100, 100})
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Content Title")
            .setContentText("Content Text");

    builder.setSmallIcon(R.drawable.bus);
    builder.setContentTitle("Your bus schedule");
    builder.setContentText("Your bus for "+ search );
    Spinner dropdown2 = findViewById(R.id.spinner3);
    Spinner dropdown3 = findViewById(R.id.spinner4);
    String join = "-";
    String ConcatedString1 = dropdown2.getSelectedItem().toString().concat(join).concat(dropdown3.getSelectedItem().toString());
    Intent intent = new Intent(this, SecondClass.class);
    intent.putExtra("username",ConcatedString1);
    PendingIntent pIntent1 = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
    PendingIntent pIntent = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.ic_prev, " helpful", pIntent1) // #0
            .addAction(R.drawable.ic_pause, "not helpful ", pIntent)  // #1

            .build();


    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(SecondClass.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);

这里的第一个按钮我已经添加了来自pintent1的动作以及来自pintent1和pintent的其他动作我有getBroadcast将重定向到notificationaction,其中数据库中的那行将被删除。

Notificationaction

 public void onReceive(Context context, Intent intent) {



    String action = intent.getStringExtra("action");
    if (action.equals("Sunday-09")) {
        performAction1(action);
    } else  {
        performAction2(action);

    }
    Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    context.sendBroadcast(it);
}

public void performAction1(String action)
{
    DatabaseHelper myDb = new DatabaseHelper(context);
    myDb.deleteData(action);
}
public void performAction2(String action)
{
    DatabaseHelper myDb = new DatabaseHelper(context);
    myDb.deleteData(action);
}

清单

  <receiver android:name=".NofificationAction"></receiver>

Databasehelper

 public void deleteData(String name) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL( "delete from search_table where name='" + name + "'");

}

但是当我点击没有帮助时,我仍然可以看到记录。谁能帮我。感谢您的建议

1 个答案:

答案 0 :(得分:0)

来自documentationexecSQL

  

执行[s]单个不是a的SQL语句   SELECT / INSERT / UPDATE / DELETE

相反,您应该使用delete