Android_Toast&Notification

Andorid: Toast



Context context = getApplicationContext();
CharSquence text = "Hello";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(content, text, duration);
toast.show();

reference:

Android APIs: Andoid Toast
Sample code: C , J 


Android: Notification

1. get the NotificationManager

      NotificationManager notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

2. setup Notification object   

        int icon = R.drawable.msn_thumb;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon,tickerText,when);

 3. Define the Notification's expanded message and Intent   

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
        notification.setLatestEventInfo(this, "My Notification", "Hello", contentIntent);

        notification.defaults = Notification.DEFAULT_ALL;


 4. Pass the Notification to the NotificationManager

        notificationmanager.notify(R.drawable.msn_thumb, notification);

reference:

Android APIs: Andorid Notification
Sample code: C,  

package com.example.toast_notifications;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class TNActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_tn, menu);
        return true;
    }

    public void toastOnClick(View tView)
    {
        Context context = getApplicationContext();
        CharSequence text = "This is a toast test";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        // set the position of toast
        toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
       
        toast.show();
    }
   
    @SuppressWarnings("deprecation")
    public void NtOnClick(View NtView)
    {
        //1. get the NotificationManager
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager notificationmanager = (NotificationManager) getSystemService(ns);
        //2. setup Notification object
        int icon = R.drawable.msn_thumb;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon,tickerText,when);
        //3. Define the Notification's expanded message and Intent
        /*
         * PendingIntent是指一個intent包裝,讓intent在某種條件下被執行。
         * 下面程式使用PendingIntent.getActivity生成PendingIntent,
         * 其中包裝了一個intent,即new Intent(this, Call.class),
         * 使得在某種條件下會執行此intent,將目前的Activity跳至Call Activity,
         * 此例之條件為點擊Notification通知欄裡的訊息。
         */
        PendingIntent contentIntent = PendingIntent.getActivity(this,
                0, new Intent(this, Call.class), 0);
        notification.setLatestEventInfo(this, "My Notification", "Hello", contentIntent);
            /*set effects of Notification
             * vibrate: Must to add <uses-permission android:name="android.permission.VIBRATE"/> into Manifest file
             * there are other ways such as sounds and lights.
             */
           //notification.defaults = Notification.DEFAULT_ALL;
        notification.sound = Uri.parse("file:///sdcard/media/audio/ringtones/ItsMyLifeRingTone.mp3");
        //4. Pass the Notification to the NotificationManager:
        notificationmanager.notify(R.drawable.msn_thumb, notification);
    }
}


      



留言