gpt4 book ai didi

android - 将传入的 whatsapp 通知存储到本地数据库

转载 作者:行者123 更新时间:2023-11-29 02:30:49 25 4
gpt4 key购买 nike

我正在构建一个 android 应用程序,它将收听 whatsapp 通知并存储它并显示它...我已经使用 notificationlistenerService 来做到这一点...我能够收听我的所有 whatsapp 通知问题是当我重新启动我的应用程序时,列表中的所有数据都丢失了;(所以我想将它保存在数据库中......以及我如何在后台运行我的应用程序我是新手(菜鸟)任何帮助任何建议感谢

对/对 enter image description here

enter image description here

public class NotificationService extends NotificationListenerService {

Context context;

@Override

public void onCreate() {

super.onCreate();
context = getApplicationContext();

}
@Override

public void onNotificationPosted(StatusBarNotification sbn) {


String pack = sbn.getPackageName();
// String ticker = sbn.getNotification().tickerText.toString();
String ticker ="";
if(sbn.getNotification().tickerText !=null) {
ticker = sbn.getNotification().tickerText.toString();
}


Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
// String text = extras.getCharSequence("android.text").toString();
int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON);
Bitmap id = sbn.getNotification().largeIcon;

String text = null;
if (extras.getCharSequence("android.text") != null) {
text = extras.getCharSequence("android.text").toString();
}
if (text == null) {
if (extras.get("android.textLines") != null) {
CharSequence[] charText = (CharSequence[]) extras
.get("android.textLines");
if (charText.length > 0) {
text = charText[charText.length - 1].toString();
}
}
}

// Log.i("Package",pack);
// Log.i("Ticker",ticker);
// Log.i("Title",title);
// Log.i("Text",text);

if(pack.equals("com.whatsapp")) {
Intent msgrcv = new Intent("Msg");
// msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
if (id !=null) {
// if (text.equals("This message was deleted")){
// text="some messages may be deleted";
// msgrcv.putExtra("text", text);
// }
ByteArrayOutputStream stream = new ByteArrayOutputStream();
id.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
msgrcv.putExtra("icon", byteArray);

LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
}
} else{
return;
}
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}

最佳答案

其实,这并不难。 但是,作为初学者(正如您所说的那样),要理解这是一个漫长的进步。

我将引导您使用 android 中的 sqlite 数据库教程 [tutorial link] .

基本上你可以使用任何其他数据库,但我建议从 sqlite 开始。

本教程解释的是如何为 sql 连接制作某种包装器(辅助类)。

public class DatabaseHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "notes_db";


public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

// create notes table
db.execSQL(Note.CREATE_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);

// Create tables again
onCreate(db);
} }

这个助手类将帮助您与本地 sqlite 数据库进行交互。

关于android - 将传入的 whatsapp 通知存储到本地数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49705729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com