Android新手之旅(3) 信息的输出

时间:2022-04-23
本文章向大家介绍Android新手之旅(3) 信息的输出,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

  不管什么语言,了解信息的输出可谓紧要的事情,如vb的msgbox,js的alert,c#的MessageBox.Show,这个对于调试意义重大。Android的输出方法有:

一、用Log输出。共分Log.v,Log.d,Log.i,Log.w,Log.e,和Log4Net差不多了,用颜色区分,在LogCat窗口中查看。

二、用AlertDialog。将弹出窗口,并可以处理返回事件

import android.app.AlertDialog;
import android.content.DialogInterface;
             new AlertDialog.Builder(login.this)
            .setTitle("这是提示!")
            .setMessage("这是提示的内容")
            .setPositiveButton("关闭",new DialogInterface.OnClickListener(){public void onClick(DialogInterface di, int ii){}})
            .show();
 

三、在信息栏显示。用Toast.makeText命令。

Toast.makeText(this,"test info",Toast.LENGTH_SHORT).show();

四、在状态栏显示。因为涉及到单击后进入另外一个Activity,所以工作量较多。 假设已经存在一个新的Acivity名为newact,参见

NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.icon, "Hello,there!", System.currentTimeMillis());              
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent i=new Intent();
i.setClass(add2.this, newact.class);
PendingIntent pi=PendingIntent.getActivity(this, 0, i, 0);
n.setLatestEventInfo(this, "button1", "button1的通知", pi);
nm.notify(R.string.app_name, n);

关于通知的更详细的设置参见