Toast and Notification, vibrate and sound : Toast « UI « Android






Toast and Notification, vibrate and sound

  


package app.test;

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.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

class ActivityToast extends Activity {

  OnClickListener listener1 = null;
  OnClickListener listener2 = null;
  Button button1;
  Button button2;
  private static int NOTIFICATIONS_ID = R.layout.activity_toast;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listener1 = new OnClickListener() {
      public void onClick(View v) {
        setTitle("Toast");
        showToast(Toast.LENGTH_SHORT);

      }
    };
    listener2 = new OnClickListener() {
      public void onClick(View v) {
        setTitle("Toast");
        showToast(Toast.LENGTH_LONG);
        showNotification();
      }
    };
    setContentView(R.layout.activity_toast);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(listener1);
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(listener2);
  }

  protected void showToast(int type) {

    View view = inflateView(R.layout.toast);

    TextView tv = (TextView) view.findViewById(R.id.content);
    tv.setText("aaa");

    Toast toast = new Toast(this);
    toast.setView(view);
    toast.setDuration(type);
    toast.show();
  }

  private View inflateView(int resource) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return vi.inflate(resource, null);
  }

  protected void showNotification() {

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    CharSequence title = "asdf";
    CharSequence contents = "yourHost.com";

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, ActivityMain.class), 0);

    Notification notification = new Notification(R.drawable.icon,
        title, System.currentTimeMillis());

    notification.setLatestEventInfo(this, title, contents, contentIntent);
    notification.vibrate = new long[] { 100, 250, 100, 500 };

    notificationManager.notify(NOTIFICATIONS_ID, notification);
  }
}

class ActivityMainNotification extends Activity {
  private static int NOTIFICATIONS_ID = R.layout.activity_notification;
  private NotificationManager mNotificationManager;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notification);

    Button button;

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    button = (Button) findViewById(R.id.sun_1);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        setWeather("A", "B", "C", R.drawable.sun);
      }
    });

    button = (Button) findViewById(R.id.cloudy_1);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        setWeather("A", "B", "C",
            R.drawable.cloudy);
      }
    });

    button = (Button) findViewById(R.id.rain_1);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        setWeather("A", "B", "C", R.drawable.rain);
      }
    });

    button = (Button) findViewById(R.id.defaultSound);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        setDefault(Notification.DEFAULT_SOUND);
      }
    });

    button = (Button) findViewById(R.id.defaultVibrate);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        setDefault(Notification.DEFAULT_VIBRATE);
      }
    });

    button = (Button) findViewById(R.id.defaultAll);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        setDefault(Notification.DEFAULT_ALL);
      }
    });

    button = (Button) findViewById(R.id.clear);
    button.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        mNotificationManager.cancel(NOTIFICATIONS_ID);
      }
    });

  }

  private void setWeather(String tickerText, String title, String content,
      int drawable) {

    Notification notification = new Notification(drawable, tickerText,
        System.currentTimeMillis());

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, ActivityMain.class), 0);

    notification.setLatestEventInfo(this, title, content, contentIntent);

    mNotificationManager.notify(NOTIFICATIONS_ID, notification);
  }

  private void setDefault(int defaults) {

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, ActivityMain.class), 0);

    String title = "t";
    String content = "c";

    final Notification notification = new Notification(R.drawable.sun,
        content, System.currentTimeMillis());

    notification.setLatestEventInfo(this, title, content, contentIntent);

    notification.defaults = defaults;

    mNotificationManager.notify(NOTIFICATIONS_ID, notification);
  }
}

public class ActivityMain extends Activity {

  OnClickListener listener1 = null;
  OnClickListener listener2 = null;

  Button button1;
  Button button2;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listener1 = new OnClickListener() {
      public void onClick(View v) {
        setTitle("Notification");
        Intent intent = new Intent(ActivityMain.this,
            ActivityMainNotification.class);
        startActivity(intent);

      }
    };
    listener2 = new OnClickListener() {
      public void onClick(View v) {
        setTitle("Toast");
        Intent intent = new Intent(ActivityMain.this,
            ActivityToast.class);
        startActivity(intent);

      }
    };
    setContentView(R.layout.main);
    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(listener1);
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(listener2);
  }
}


//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Notification" />
  <Button android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Toast" />
</LinearLayout>
//toast.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/toast_frame">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/default_icon"
                />

            <TextView
                android:id="@+id/content"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="6dip"
                />

    </LinearLayout>
</FrameLayout>
//activity_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Short Toast" />
  <Button android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Long Toast" />
</LinearLayout>
//activity_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
            
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        
            <Button
                android:id="@+id/sun_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="A" />
        
            <Button
                android:id="@+id/cloudy_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="B" />
                
              <Button
                android:id="@+id/rain_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="C" />
                
        </LinearLayout>
        
         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:text="Advanced notification" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        
            <Button
                android:id="@+id/defaultSound"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sound notification" />
        
            <Button
                android:id="@+id/defaultVibrate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vibarate notification" />
                
              <Button
                android:id="@+id/defaultAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vibrate and Sound notification" />
                
        </LinearLayout>
        
        <Button android:id="@+id/clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dip"
            android:text="Remove notification" />
            
    </LinearLayout>

</ScrollView>

   
    
  








Related examples in the same category

1.Raise a Toast
2.Toast Message Demo
3.Using Toast.LENGTH_SHORT
4.Show Toast
5.Make Toast Text
6.Responsible for sending Toasts under all circumstances.
7.Toast.LENGTH_SHORT
8.AlertDialog vs Toast
9.Show long,short Toast
10.The Toast util class
11.show Toast in a Thread
12.Popup Debug Message