Toast分析
2019-11-14 本文已影响0人
瀚海来客
自定义Toast
1.自定义Toast相当于自己做了makeText()方法的工作
源码位置:frameworks/base/core/java/Android/widght/Toast.java
Toast#makeText()
/**
* Make a standard toast to display using the specified looper.
* If looper is null, Looper.myLooper() is used.
* @hide
*/
public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
@NonNull CharSequence text, @Duration int duration) {
Toast result = new Toast(context, looper);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
2.show()方法中内容
灵魂三问
- 通过getService()怎么就获得一个INotificationManager对象?
- TN类是个什么鬼?
- 方法最后只有一个service.enqueueToast(),显示和隐藏在哪里?
public void show() {
if (mNextView == null) {
throw new RuntimeException("setView must have been called");
}
INotificationManager service = getService();
String pkg = mContext.getOpPackageName();
TN tn = mTN;
tn.mNextView = mNextView;
try {
service.enqueueToast(pkg, tn, mDuration);
} catch (RemoteException e) {
// Empty
}
}