问题描述
我的应用程序将显示所有包名一一定制敬酒,但问题是,我不希望干杯应用即外所示,应用程序退出时。我如何结束我的循环或以某种方式从显示的onDestroy上停止祝酒词?
公共类MainActivity延伸活动{ 浮X1,X2;
浮Y1,Y2; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); //显示首次安装
共享preferences设置= getShared preferences(preFS,0);
布尔firstRun = settings.getBoolean(firstRun,FALSE);
如果(firstRun == FALSE)//如果第一次运行
{
共享preferences.Editor编辑= settings.edit();
editor.putBoolean(firstRun,真正的);
editor.commit();
意向意图=新意图(这一点,Setup1.class);
startActivity(意向);
}其他{
}
// ** // //通知活化剂
//从ReceiverReminder消防通知
意向意图=新意图(这一点,ReceiverReminder.class);
发件人的PendingIntent = PendingIntent.getBroadcast(这一点,0,意向,
PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager AM =(AlarmManager)本
.getSystemService(Context.ALARM_SERVICE);
长期经常性=(1 * 10000); //以毫秒为单位
am.setRepeating(AlarmManager.RTC,Calendar.getInstance()
.getTimeInMillis(),经常性,发件人);
// //从调度日报消防通知
意图intent1 =新意图(这一点,ReceiverSchedulerDaily.class);
的PendingIntent sender1 = PendingIntent.getBroadcast(这一点,0,intent1,
PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager AM1 =(AlarmManager)本
.getSystemService(Context.ALARM_SERVICE);
长recurring1 =(3 * 60000); //以毫秒为单位
am1.setRepeating(AlarmManager.RTC,Calendar.getInstance()
.getTimeInMillis(),recurring1,sender1);
//
//从调度消防通知3天
意图intent11 =新意图(这一点,ReceiverSchedulerThreeDays.class);
的PendingIntent sender11 = PendingIntent.getBroadcast(这一点,0,intent11,
PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager AM11 =(AlarmManager)本
.getSystemService(Context.ALARM_SERVICE);
长recurring11 =(1 * 10000); //以毫秒为单位
am11.setRepeating(AlarmManager.RTC,Calendar.getInstance()
.getTimeInMillis(),recurring11,sender11);
//
//从调度消防通知7天
意图intent111 =新意图(这一点,ReceiverSchedulerWeekly.class);
的PendingIntent sender111 = PendingIntent.getBroadcast(这一点,0,
intent111,PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager am111 =(AlarmManager)本
.getSystemService(Context.ALARM_SERVICE);
长recurring111 =(1 * 10000); //以毫秒为单位
am111.setRepeating(AlarmManager.RTC,Calendar.getInstance()
.getTimeInMillis(),recurring111,sender111);
// - ** - // } 公共布尔onTouchEvent(MotionEvent的TouchEvent){
开关(touchevent.getAction()){
//当用户第一次触摸屏幕,我们得到x和y坐标
案例MotionEvent.ACTION_DOWN:{
X1 = touchevent.getX();
Y1 = touchevent.getY();
打破;
}
案例MotionEvent.ACTION_UP:{
X2 = touchevent.getX();
Y2 = touchevent.getY(); 如果离开屏幕右侧掠过事件//
如果(X1< X2){
意向意图=新意图(这一点,UiNetMon.class);
startActivity(意向);
overridePendingTransition(R.anim.slide_right_out,
R.anim.slide_right_in);
完();
} //如果正确的在屏幕上向左扫事件
如果(X1> X2){
意向意图=新意图(这一点,UiScheduler.class);
startActivity(意向);
overridePendingTransition(R.anim.slide_left_in,
R.anim.slide_left_out);
完();
}
打破;
}
}
返回false;
} 公共无效优化(查看视图){ //清理所有的应用程序缓存
软件包管理系统下午= getPackageManager();
方法[] =方法pm.getClass()getDeclaredMethods()。
为(M法:方法){
如果(m.getName()。等于(freeStorageAndNotify)){
尝试{
长desiredFreeStorage = Long.MAX_VALUE;
m.invoke(时许,desiredFreeStorage,NULL);
}赶上(例外五){
}
打破;
}
}
//
//启用,禁用无线网络
WifiManager wifiManager =(WifiManager)本
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(假);
wifiManager.setWifiEnabled(真);
//
//进程杀手和显示所有敬酒包名
ActivityManager actvityManager =(ActivityManager)getApplicationContext()
.getSystemService(getApplicationContext()ACTIVITY_SERVICE);
清单< RunningAppProcessInfo> procInfos = actvityManager
.getRunningAppProcesses(); 对(INT PNUM = 0; PNUM&下; procInfos.size(); PNUM ++){
//如果
//((procInfos.get(PNUM))。processName.contains(机器人)// prevent
//系统免受杀害的应用
// || (procInfos.get(PNUM))。processName.contains(系统)
// || (procInfos.get(PNUM))。processName.contains(华为)
// || (procInfos.get(PNUM))。processName.contains(阿迪尔)){
// // Toast.makeText(getApplicationContext(),系统应用
// // Toast.LENGTH_SHORT).show();
//}其他{
actvityManager
.killBackgroundProcesses(procInfos.get(PNUM).processName);
LayoutInflater吹气= getLayoutInflater();
查看布局= inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup中)findViewById(R.id.toast_layout_root));
TextView的文本=(TextView的)layout.findViewById(R.id.text);
text.setText(优化+ procInfos.get(PNUM).processName); 吐司面包=新吐司(getApplicationContext());
toast.setGravity(Gravity.BOTTOM,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(布局);
toast.show();
//
}
} @覆盖
保护无效的onDestroy(){
super.onDestroy(); }
}
您有很多的面包,因为它们是在一个循环中创建的。你只需要取消他们在的onDestroy
。
...
mPendingToasts.clear();
对(INT PNUM = 0; PNUM&下; procInfos.size(); PNUM ++){
...
吐司面包=新吐司(getApplicationContext());
...
mPendingToasts.add(面包);
toast.show(); }
}私人的ArrayList<&吐司GT; mPendingToasts =新的ArrayList<&吐司GT;();
@覆盖
保护无效的onDestroy(){
对于(吐司面包:mPendingToasts){
toast.cancel();
}
super.onDestroy();}
请注意,取消吐司只是prevent它们被显示。在循环(即杀PROC)逻辑将被执行的。 (即呈现出Toast是异步)
my app displays all package names one by one in a custom toast, but the problem is, i don't want the toast to be shown outside the app i.e, when the app exits. how do i end my for loop or somehow stop the toasts from showing up in onDestroy?
public class MainActivity extends Activity {
float x1, x2;
float y1, y2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// show first time setup
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", false);
if (firstRun == false)// if running for first time
{
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", true);
editor.commit();
Intent intent = new Intent(this, Setup1.class);
startActivity(intent);
} else {
}
// **//
// NOTIFICATION ACTIVATOR
// Fire Notification from ReceiverReminder
Intent intent = new Intent(this, ReceiverReminder.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
long recurring = (1 * 10000); // in milliseconds
am.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis(), recurring, sender);
//
// Fire Notification from Scheduler Daily
Intent intent1 = new Intent(this, ReceiverSchedulerDaily.class);
PendingIntent sender1 = PendingIntent.getBroadcast(this, 0, intent1,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am1 = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
long recurring1 = (3 * 60000); // in milliseconds
am1.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis(), recurring1, sender1);
//
// Fire Notification from Scheduler 3 Days
Intent intent11 = new Intent(this, ReceiverSchedulerThreeDays.class);
PendingIntent sender11 = PendingIntent.getBroadcast(this, 0, intent11,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am11 = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
long recurring11 = (1 * 10000); // in milliseconds
am11.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis(), recurring11, sender11);
//
// Fire Notification from Scheduler 7 Days
Intent intent111 = new Intent(this, ReceiverSchedulerWeekly.class);
PendingIntent sender111 = PendingIntent.getBroadcast(this, 0,
intent111, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am111 = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
long recurring111 = (1 * 10000); // in milliseconds
am111.setRepeating(AlarmManager.RTC, Calendar.getInstance()
.getTimeInMillis(), recurring111, sender111);
// --**--//
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = touchevent.getX();
y2 = touchevent.getY();
// if left to right sweep event on screen
if (x1 < x2) {
Intent intent = new Intent(this, UiNetMon.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_right_out,
R.anim.slide_right_in);
finish();
}
// if right to left sweep event on screen
if (x1 > x2) {
Intent intent = new Intent(this, UiScheduler.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_left_in,
R.anim.slide_left_out);
finish();
}
break;
}
}
return false;
}
public void optimize(View view) {
// clean all app caches
PackageManager pm = getPackageManager();
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("freeStorageAndNotify")) {
try {
long desiredFreeStorage = Long.MAX_VALUE;
m.invoke(pm, desiredFreeStorage, null);
} catch (Exception e) {
}
break;
}
}
//
// enable, disable wifi
WifiManager wifiManager = (WifiManager) this
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
wifiManager.setWifiEnabled(true);
//
// Process Killer and display all package names in toast
ActivityManager actvityManager = (ActivityManager) getApplicationContext()
.getSystemService(getApplicationContext().ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager
.getRunningAppProcesses();
for (int pnum = 0; pnum < procInfos.size(); pnum++) {
// if
// ((procInfos.get(pnum)).processName.contains("android")//prevent
// system apps from getting killed
// || (procInfos.get(pnum)).processName.contains("system")
// || (procInfos.get(pnum)).processName.contains("huawei")
// || (procInfos.get(pnum)).processName.contains("adil")) {
// // Toast.makeText(getApplicationContext(), "system apps",
// // Toast.LENGTH_SHORT).show();
// } else {
actvityManager
.killBackgroundProcesses(procInfos.get(pnum).processName);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("optimized" + procInfos.get(pnum).processName);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
//
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
You have many Toast because they are created in a loop. You just have to cancel them in the onDestroy
.
...
mPendingToasts.clear();
for (int pnum = 0; pnum < procInfos.size(); pnum++) {
...
Toast toast = new Toast(getApplicationContext());
...
mPendingToasts.add(toast);
toast.show();
}
}
private ArrayList<Toast> mPendingToasts = new ArrayList<Toast>();
@Override
protected void onDestroy() {
for(Toast toast:mPendingToasts){
toast.cancel();
}
super.onDestroy();
}
Be aware that cancelling the Toast will just prevent them from being shown. The logic in your loop (i.e. killing proc) will be executed anyway. (i.e. showing a Toast is asynchronous)
这篇关于取消举杯机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!