簡體   English   中英

Android狀態欄通知中的自定義內容

[英]Custom content in Android Status bar notification

我正在嘗試從另一個活動的textview中獲取內容,以顯示在通知欄消息中。 它有效,但不能正確執行。 textview中的字符串確實顯示在通知中,而我是通過將其他活動的textview中的信息捆綁在一起,然后讓通知管理器獲取該捆綁包來實現的。 當啟動另一個活動時,就會出現問題,因為該活動中的最后一段代碼會進行捆綁和發送,因此會觸發通知,而忽略設置的觸發時間,從而觸發通知。 所以我的問題是,讓通知從另一個活動中獲取字符串的最佳和最簡便的方法什么? 這是活動,這是問題所在。 它自行觸發通知:

    import java.io.IOException;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.widget.TextView;

public class DBTest2 extends Activity {

String scrNote;
TextView showBV;
NotificationManager nm;
DBAdapter dba;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dbtest_2);
    showBV = (TextView) findViewById(R.id.getBK_TV);

    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //---cancel the notification---
    try{
    nm.cancel(getIntent().getExtras().getInt("uID"));
    } catch (Exception e) {
        System.out.println("Error when cancelling: "+e.toString());
    }
    //---END cancel the notification---



    //---- SHOW IN NOTIFICATION------

    scrNote = showBV.getText().toString();
    Bundle moveScrNote = new Bundle();
    moveScrNote.putString("mSN", scrNote);
    Intent toNoteBody = new Intent(DBTest2.this, DisplayNotifications.class);
    toNoteBody.putExtras(moveScrNote);
    startActivity(toNoteBody);


    //---- END   SHOW IN NOTIFICATION------


}


}

這是通知管理器:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //---get the notification ID for the notification; 
    // passed in by the MainActivity---
    int uID = getIntent().getExtras().getInt("uniqueID");

    //---PendingIntent to launch activity
    Intent noteI = new Intent("com.vee.search01.DBTEST2");
    noteI.putExtra("uniqueID", uID);

    PendingIntent herroIntent = 
        PendingIntent.getActivity(this, 0, noteI, 0);

    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    long fireTime = System.currentTimeMillis();
    String noteTitle = "Notification Title";

    Bundle getNoteBody = getIntent().getExtras();
    String gotNoteBody = getNoteBody.getString("mSN");
    String noteBody = gotNoteBody;

    Notification note = new Notification(R.drawable.noteicon, noteTitle, fireTime);
    note.setLatestEventInfo(this, noteTitle, noteBody, herroIntent);
    note.defaults |= Notification.DEFAULT_SOUND;
    note.defaults |= Notification.FLAG_SHOW_LIGHTS;
    nm.notify(uID, note);
    finish();
}

}

在活動之間傳輸內容的最佳方法是通過Intent中的附加內容發送內容。

如果要從活動A發出通知,並且要在活動B中處理它,則在A中創建通知,並插入一個包含啟動B的意圖的PendingIntent。當顯示通知並用戶單擊它時,B應該被辭退了。

如果要將通知文本從B發送到A,請使用單獨的Intent。

如果您試圖將通知Intent的文本發送到B並顯示通知,請將該文本放入Intent的Extras中。

另外,如果您使用的是該平台的最新版本,請閱讀Notification的參考文檔。 為了支持通過Notification.Builder創建通知,不建議使用該方法。 優點之一是您可以將通知設置為自動取消,因此您不必在代碼中取消通知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM