[Android]訊息:Log, Toast, notify
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());
================================
分享與讚美,是我們繼續打拼的原動力.
若文章對您有幫助,望請不吝按讚或分享.
或者對影片有興趣可以訂閱頻道接收通知
================================
YouTube 頻道
FB 粉絲專頁
================================