android SDK中提供了Log類別輸出訊息
Log.v("log_msg", "verbose: this is debug_message");Log類別中的訊息有五種 , 可以根據重要程度自行決定該使用哪一種
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");
在Eclipse的Logcat視窗中 , 就能夠看到這些訊息
並且不同訊息有著不同的顏色:
利用Toast也能輸出訊息, 而且這些訊息可以直接顯示在螢幕上面
除了文字以外, Layout也能透過Toast顯示
第一種方法:(直接秀文字)
第二種方法:(顯示layout)Toast toast2=Toast.makeText(this, "This is a toast msg", Toast.LENGTH_LONG);toast2.setGravity(Gravity.CENTER_HORIZONTAL, 5, -5);toast2.show();
另外也能夠在status bar顯示訊息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();
這種訊息一定要包含: 小圖, 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());
範例程式下載
ps.官網說明文件 有提到:
由於setLatestEventInfoThis method was deprecated in API level 11(ANdroid 3.0).所以用
Notification.Builder
instead.
沒有留言:
張貼留言