Android 台灣中文網

標題: 幾個輸出訊息的方式 [打印本頁]

作者: xyaw    時間: 2013-8-1 10:31
標題: 幾個輸出訊息的方式
debug時加入訊息可以幫助瞭解程式運行情形
android SDK中提供了Log類別輸出訊息
Log.v("log_msg", "verbose: this is debug_message");
Log.d("log_msg", "debug:   this is debug_message");
Log.i("log_msg",  "info:    this is debug_message");
Log.w("log_msg", "warn:    this is debug_message");
Log.e("log_msg", "error:   this is debug_message");
Log類別中的訊息有五種 , 可以根據重要程度自行決定該使用哪一種
在Eclipse的Logcat視窗中  , 就能夠看到這些訊息
並且不同訊息有著不同的顏色:


利用Toast也能輸出訊息, 而且這些訊息可以直接顯示在螢幕上面
除了文字以外, Layout也能透過Toast顯示

第一種方法:(直接秀文字)
Toast toast2=Toast.makeText(this, "This is a toast msg", Toast.LENGTH_LONG);
toast2.setGravity(Gravity.CENTER_HORIZONTAL, 5, -5);
toast2.show();

第二種方法:(顯示layout)
LayoutInflater inflater= getLayoutInflater();
View toastLayout = inflater.inflate(R.layout.toast_robot,                                       (ViewGroup)findViewById(R.id.toast_robot_id)  );
Toast toast3=new Toast(this);
toast3.setGravity(Gravity.CENTER, 0, 0);
toast3.setDuration(Toast.LENGTH_LONG);
toast3.setView(toastLayout);
toast3.show();

另外也能夠在status bar顯示訊息
這種訊息一定要包含:  小圖, Title, 訊息文字,如下圖

Code部分:
Context context= getApplicationContext();
CharSequence ttile = "提醒的標題";
CharSequence ttext2 = "點擊進入詳細內容";

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                                       .setSmallIcon(R.drawable.ic_launcher)
                                       .setContentTitle(ttile)
                                       .setContentText(ttext2);
  
Intent resultIntent = new Intent(this, notify_activity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(notify_activity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());

範例程式就自行去下載囉
http://guangyaw.blogspot.tw/2013/07/androidlog-toast-notify.html


作者: whl    時間: 2013-8-1 22:45
不錯喔,下次多分享一些。




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1