我正在构建一个Android应用程序,它将监听whatsapp通知并存储并显示它...我已经使用notificationlistenerService
来做...我能够听到所有whatsapp通知我的问题是什么时候我重启我的应用程序列表中的所有数据都丢失了;(所以我想将它保存在数据库中......我怎么能在后台运行我的应用程序我是新的(noob)任何帮助任何建议赞赏
O / P
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");
}
}
答案 0 :(得分:0)
实际上,这不是一项艰巨的任务。 但是,作为初学者的理解是一个很长的进步(正如你所说的那样)。
我将带您进入android [tutorial link]的wrapper
数据库教程。
基本上你可以使用任何其他数据库,但我建议从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
此助手类将帮助您与本地Controller
数据库进行交互。