简体   繁体   English

Android状态栏通知中的自定义内容

[英]Custom content in Android Status bar notification

I'm trying to get content from a textview in another activity to show up in a notification bar message. 我正在尝试从另一个活动的textview中获取内容,以显示在通知栏消息中。 It works, but not correctly. 它有效,但不能正确执行。 The string from the textview does show up in the notification, and I'm doing that by bundling the info from the other activities' textview and then have the notification manager grab the bundle. textview中的字符串确实显示在通知中,而我是通过将其他活动的textview中的信息捆绑在一起,然后让通知管理器获取该捆绑包来实现的。 The problem arises when the other activity is launched, it fires off the notification because the last chunk of code in the activity does the bundling and sending, which causes the notification to fire, ignoring the set firing time. 当启动另一个活动时,就会出现问题,因为该活动中的最后一段代码会进行捆绑和发送,因此会触发通知,而忽略设置的触发时间,从而触发通知。 So my question is What's the best and easiest way to have a notification grab a string from another activity? 所以我的问题是,让通知从另一个活动中获取字符串的最佳和最简便的方法什么? Here's the activity, which is the problem. 这是活动,这是问题所在。 It fires the notification on it's own: 它自行触发通知:

    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------


}


}

and here is the notification manager: 这是通知管理器:

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();
}

}

The best way to transfer content between activities is to send it via extras in an Intent. 在活动之间传输内容的最佳方法是通过Intent中的附加内容发送内容。

If you're issuing a notification from Activity A, and you want to handle it in Activity B, then create the notification in A and insert a PendingIntent containing an Intent to start B. When the notification displays and the user clicks it, B should be fired. 如果要从活动A发出通知,并且要在活动B中处理它,则在A中创建通知,并插入一个包含启动B的意图的PendingIntent。当显示通知并用户单击它时,B应该被辞退了。

If you want to send the notification text from B to A, use a separate Intent. 如果要将通知文本从B发送到A,请使用单独的Intent。

If you're trying to send the text of the notification Intent to B as well as display the notification, put the text in the Intent's extras. 如果您试图将通知Intent的文本发送到B并显示通知,请将该文本放入Intent的Extras中。

Also, if you're on a recent version of the platform, go read the reference docs for Notification. 另外,如果您使用的是该平台的最新版本,请阅读Notification的参考文档。 It's been deprecated in favor of creating notifications through Notification.Builder. 为了支持通过Notification.Builder创建通知,不建议使用该方法。 One advantage is that you can set the notification to auto-cancel, so you don't have to cancel it in code. 优点之一是您可以将通知设置为自动取消,因此您不必在代码中取消通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM