在Android通知中需要帮助

时间:2014-05-04 05:47:17

标签: android notifications

我正在开发一款应用 我必须出示通知, 我表现得很成功但是我想要 当用户点击通知时,它应拨打手机号码......

MyNotify.java

    package com.premiumtechhelp.net;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;





        public class Mynotify extends Activity {

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                TextView txt = new TextView(this);

                txt.setText("Some text here");
                setContentView(txt);
            }
        }

MainActivity.java

private static final int NOTIFY_ME_ID = 1337;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final NotificationManager mgr = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note = new Notification(R.drawable.ic_launcher,
                "Hello Notifi", System.currentTimeMillis());
        PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this,
                Mynotify.class), 0);
        note.setLatestEventInfo(this, "Hello text",
                "mobile number 9906012345", i);
        mgr.notify(NOTIFY_ME_ID, note);

1 个答案:

答案 0 :(得分:0)

您需要将数字作为额外内容添加到创建通知的意图中:

final String number = "1234567890";
final NotificationManager mgr = (NotificationManager)this.getSystemService(
                                                   Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.ic_launcher, "Hello Notifi",
                                     System.currentTimeMillis());
Intent intent = new Intent(this, Mynotify.class);
intent.putExtra("number", number);
PendingIntent i = PendingIntent.getActivity(this, 0, intent, 0);
nte.setLatestEventInfo(this, "Hello text", "mobile number " + number, i);
mgr.notify(NOTIFY_ME_ID, note);

你可以通过以下方式在活动中使用:

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView txt = new TextView(this);
    String number = getIntent().getStringExtra("number");
    txt.setText("Some text here " + number);
    setContentView(txt);
}