綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[教程] 幾個輸出訊息的方式

[複製連結] 查看: 979|回覆: 1|好評: 0
跳轉到指定樓層
樓主
xyaw | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-8-1 10:31

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
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

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
whl | 收聽TA | 只看該作者
發表於 2013-8-1 22:45
不錯喔,下次多分享一些。
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則