简书链接:打造activity的toast
文章字数:44,阅读全文大约需要1分钟

plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


/**
* Created by qssq on 2018/7/31 qssq666@foxmail.com
*/
public class ActivityToast extends Activity {

@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


getWindow().getAttributes().flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
getWindow().getAttributes().format = PixelFormat.TRANSLUCENT;

Intent intent = getIntent();

String title = intent.getStringExtra("title");
int duration = intent.getIntExtra("duration", 1000);

FrameLayout frameLayout = new FrameLayout(this);

TextView view = new TextView(this);
view.setText(title);
view.setPadding(30, 10, 10, 10);
frameLayout.addView(view);
setContentView(frameLayout);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();

}
}, duration);



}

public static void showToast(Context context, String message, int duration) {
Intent intent = new Intent(context, ActivityToast.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("title", message);
intent.putExtra("duration", duration);
context.startActivity(intent);
}
}

缺点明显,影响返回键操作。影响任务栈,因此不可行。