簡體   English   中英

在Android通知中需要幫助

[英]Need Help in 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);

您需要在創建通知的意圖中添加額外的數字:

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

暫無
暫無

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

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