Android的提神活动 [英] android refreshing activity
本文介绍了Android的提神活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我想每一秒刷新我的活动。我做了以下的方式,但它不工作请指导。这里是code
公共类MyActivity延伸活动{
preFS我的preFS = NULL;
私人XYPlot mySimpleXYPlot;
号码[] series1Numbers =新数[10];
号码[] series2Numbers =新数[10];
诠释A,B,C,D,EE,F,G;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.graphval); 意向意图=新意图(这一点,MyActivity的.class);
发件人的PendingIntent = PendingIntent.getService(这一点,0,意向,0);
长firstTime = SystemClock.elapsedRealtime();
firstTime + = 1 * 60 * 1000;
AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,1 * 60 * 1000,发送者);
mySimpleXYPlot =(XYPlot)findViewById(R.id.mySimpleXYPlot); this.my preFS =新的preFS(getApplicationContext()); ArrayList的<&HashMap的LT;弦乐,对象>> MYLIST =新的ArrayList<&HashMap的LT;弦乐,对象>>();
JSONObject的JSON = JSONfunctions.getJSONfromURL(............); 尝试{ JSONArray地震= json.getJSONArray(图); 的for(int i = 0; I< earthquakes.length();我++){
HashMap的<弦乐,对象>地图=新的HashMap<弦乐,对象>();
JSONObject的E = earthquakes.getJSONObject(I)
一个=的Integer.parseInt(e.getString(sensor_a));
B =的Integer.parseInt(e.getString(sensor_b));
C =的Integer.parseInt(e.getString(sensor_c));
D =的Integer.parseInt(e.getString(sensor_d));
EE =的Integer.parseInt(e.getString(sensor_e));
F =的Integer.parseInt(e.getString(sensor_f));
G =的Integer.parseInt(e.getString(sensor_g)); }
series1Numbers [0] =一个;
series1Numbers [1] = B;
series1Numbers [2] = C;
series1Numbers [3] = d的;
series1Numbers [4] = ee值;
series1Numbers [5] = F;
series1Numbers [6] =克, //开启上面的阵列到XYSeries:
XYSeries系列1 =新SimpleXYSeries(
Arrays.asList(series1Numbers),// SimpleXYSeries需要的列表,以便把我们的数组列表
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,// Y_VALS_ONLY意味着使用元素索引的x值
系列1); //设置一系列的显示标题 //如上面一样的,系列二
XYSeries系列2 =新SimpleXYSeries(Arrays.asList(series2Numbers),SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,
系列2); //创建一个格式化用于绘制使用LineAndPointRenderer系列:
LineAndPointFormatter series1Format =新LineAndPointFormatter(
Color.rgb(0,200,0),//线的颜色
Color.rgb(0,100,0),//点色
Color.rgb(150,190,150)); //填充颜色(可选) //添加系列1到xyplot:
mySimpleXYPlot.addSeries(系列1,series1Format); //如上面一样,用系列2:
mySimpleXYPlot.addSeries(系列2,新LineAndPointFormatter(Color.rgb(0,0,200),Color.rgb(0,0,100),
Color.rgb(150,150,190)));
//减少范围的标签数
mySimpleXYPlot.setTicksPerRangeLabel(3); //默认情况下,AndroidPlot显示开发者指南铺设出你的阴谋,以帮助。
//摆脱他们的调用disableAllMarkup():
mySimpleXYPlot.disableAllMarkup(); }赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
}
}}
解决方案
您可以使用的处理来刷新你的活动。
公共类MyActivity延伸活动{
处理程序处理程序=新的处理程序();
...
公共无效的onCreate(捆绑savedInstanceState){
...
handler.postDelayed(可运行,1000);
...
} .... 私人Runnable接口可运行=新的Runnable(){
公共无效的run(){
如果(活动有效){
//做一些更新
handler.postDelayed(这一点,1000);
}
}
};
....
}
Hello i want to refresh my activity by every second . i am doing in the following way but its not working please guide . Here is the code
public class MyActivity extends Activity {
Prefs myprefs = null;
private XYPlot mySimpleXYPlot;
Number[] series1Numbers=new Number[10];
Number[] series2Numbers=new Number[10];
int a,b,c,d,ee,f,g;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graphval);
Intent intent = new Intent(this, MyActivity .class);
PendingIntent sender = PendingIntent.getService(this, 0, intent, 0);
long firstTime = SystemClock.elapsedRealtime();
firstTime += 1 * 60 * 1000;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,1 * 60 * 1000, sender);
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
this.myprefs = new Prefs(getApplicationContext());
ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();
JSONObject json = JSONfunctions.getJSONfromURL("............");
try{
JSONArray earthquakes = json.getJSONArray("graphs");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject e = earthquakes.getJSONObject(i);
a = Integer.parseInt(e.getString("sensor_a"));
b = Integer.parseInt(e.getString("sensor_b"));
c = Integer.parseInt(e.getString("sensor_c"));
d = Integer.parseInt(e.getString("sensor_d"));
ee = Integer.parseInt(e.getString("sensor_e"));
f = Integer.parseInt(e.getString("sensor_f"));
g = Integer.parseInt(e.getString("sensor_g"));
}
series1Numbers[0]=a;
series1Numbers[1]=b;
series1Numbers[2]=c;
series1Numbers[3]=d;
series1Numbers[4]=ee;
series1Numbers[5]=f;
series1Numbers[6]=g;
// Turn the above arrays into XYSeries:
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Series1"); // Set the display title of the series
// Same as above, for series2
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,
"Series2");
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 200, 0), // line color
Color.rgb(0, 100, 0), // point color
Color.rgb(150, 190, 150)); // fill color (optional)
// Add series1 to the xyplot:
mySimpleXYPlot.addSeries(series1, series1Format);
// Same as above, with series2:
mySimpleXYPlot.addSeries(series2, new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100),
Color.rgb(150, 150, 190)));
// Reduce the number of range labels
mySimpleXYPlot.setTicksPerRangeLabel(3);
// By default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
} }
解决方案
You could use a handler to refresh you activity.
public class MyActivity extends Activity {
Handler handler = new Handler();
...
public void onCreate(Bundle savedInstanceState) {
...
handler.postDelayed(runnable, 1000);
...
}
....
private Runnable runnable = new Runnable() {
public void run() {
if(activity is active) {
// Do some updates
handler.postDelayed(this, 1000);
}
}
};
....
}
这篇关于Android的提神活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文