在Android中的计时器内运行异步任务
我正在开发一个基本的聊天类型应用程序,目前正在运行以下代码:在Android中的计时器内运行异步任务,android,timer,background,Android,Timer,Background,我正在开发一个基本的聊天类型应用程序,目前正在运行以下代码: class GetMsgs extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MsgViewActivity.this
class GetMsgs extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MsgViewActivity.this);
pDialog.setMessage("Fetching..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
Intent intent = getIntent();
String tmp = intent.getStringExtra("Header");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Header", tmp));
// getting JSON Object
// Note that create product url accepts POST method
json = jsonParser.makeHttpRequest(url,
"POST", params);
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
ListView listView_msg = (ListView)findViewById(R.id.MsgView);
int j = 0;
String Msgs[];
try{
msgs = json.getJSONArray("Messages");
auths = json.getJSONArray("Authors");
id = json.getString("ID");
Msgs = new String[msgs.length()];
for(int i = 0; i < msgs.length(); i++){
if ((msgs.getString(i) != "")&&(auths.getString(i) != "")){
Msgs[j++] = auths.getString(i) + " : " + msgs.getString(i);
}
}
msg_adapter = new ArrayAdapter<String>(MsgViewActivity.this,
android.R.layout.simple_list_item_1, Msgs);
listView_msg.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView_msg.setAdapter(msg_adapter);
}
catch(JSONException e){
e.printStackTrace();
}
}
}
当我在android中的活动开始时运行这个程序,甚至只需点击一个按钮,它就可以正常工作并完美执行。但是,当我试图以给定的时间间隔运行它以刷新消息屏幕时,应用程序崩溃了
我尝试了我在这里找到的方法:
(一)
(ii)
但我的应用程序不断崩溃
由于某种原因,不可能在特定的时间间隔运行代码,或者这是某种实现问题
一些帮助将不胜感激
日志:
04-06 11:45:40.396: V/Provider/Setting(5644): invalidate [system]: current 286 != cached 0
04-06 11:45:40.406: V/Provider/Setting(5644): from db cache, name = sound_effects_enabled value = 1
04-06 11:45:40.407: V/InputMethodManager(5644): focusOut: android.widget.EditText@41c27350 mServedView=android.widget.EditText@41c27350 winFocus=true
04-06 11:45:40.456: D/dalvikvm(5644): create interp thread : stack size=32KB
04-06 11:45:40.456: D/dalvikvm(5644): create new thread
04-06 11:45:40.456: D/dalvikvm(5644): new thread created
04-06 11:45:40.456: D/dalvikvm(5644): update thread list
04-06 11:45:40.456: D/dalvikvm(5644): threadid=11: interp stack at 0x52af2000
04-06 11:45:40.457: D/dalvikvm(5644): threadid=11: created from interp
04-06 11:45:40.457: D/dalvikvm(5644): start new thread
04-06 11:45:40.457: D/dalvikvm(5644): threadid=11: notify debugger
04-06 11:45:40.457: D/dalvikvm(5644): threadid=11 (AsyncTask #1): calling run()
04-06 11:45:40.529: V/InputMethodManager(5644): Starting input: view=android.widget.TabHost@41c23ab0
04-06 11:45:40.529: V/InputMethodManager(5644): Starting input: tba=android.view.inputmethod.EditorInfo@41c68d80 ic=null
04-06 11:45:40.529: V/InputMethodManager(5644): START INPUT: android.widget.TabHost@41c23ab0 ic=null tba=android.view.inputmethod.EditorInfo@41c68d80 controlFlags=#100
04-06 11:45:40.531: V/InputMethodManager(5644): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@41c698a0 com.touchtype.swiftkey/com.touchtype.KeyboardService #4807}
04-06 11:45:40.534: V/InputMethodManager(5644): onWindowFocus: null softInputMode=288 first=true flags=#1820002
04-06 11:45:40.534: V/InputMethodManager(5644): Not IME target window, ignoring
04-06 11:45:40.540: D/dalvikvm(5644): threadid=12: interp stack at 0x537ff000
04-06 11:45:40.542: D/libc-netbsd(5644): getaddrinfo: myphptestsite.bugs3.com get result from proxy >>
04-06 11:45:40.549: I/System.out(5644): propertyValue:true
04-06 11:45:40.550: I/System.out(5644): [socket][0] connection /93.188.160.82:80;LocalPort=45152(0)
04-06 11:45:40.551: I/System.out(5644): [CDS]connect[/93.188.160.82:80] tm:90
04-06 11:45:40.552: D/Posix(5644): [Posix_connect Debug]Process com.example.groupchat :80
04-06 11:45:40.925: I/SurfaceTextureClient(5644): [STC::queueBuffer] (this:0x51247728) fps:11.83, dur:1014.51, max:391.89, min:15.83
04-06 11:45:41.562: I/SurfaceTextureClient(5644): [STC::queueBuffer] (this:0x52afe8e0) fps:54.92, dur:1001.41, max:60.19, min:7.67
04-06 11:45:41.912: I/System.out(5644): [socket][/100.69.10.243:45152] connected
04-06 11:45:41.913: I/System.out(5644): [CDS]rx timeout:0
04-06 11:45:41.922: I/System.out(5644): >doSendRequest
04-06 11:45:41.930: I/System.out(5644): <doSendRequest
04-06 11:45:42.573: I/SurfaceTextureClient(5644): [STC::queueBuffer] (this:0x52afe8e0) fps:56.37, dur:1011.19, max:23.49, min:12.03
04-06 11:45:43.365: D/dalvikvm(5644): GC_CONCURRENT freed 211K, 12% free 13462K/15171K, paused 2ms+2ms, total 17ms
04-06 11:45:43.372: D/OpenGLRenderer(5644): Flushing caches (mode 0)
04-06 11:45:43.439: I/SurfaceTextureClient(5644): [STC::queueBuffer] (this:0x51247728) fps:5.57, dur:2514.10, max:2279.30, min:12.11
04-06 11:45:43.455: V/InputMethodManager(5644): onWindowFocus: android.widget.ListView@41c2fd98 softInputMode=16 first=false flags=#1810100
04-06 11:45:43.456: V/InputMethodManager(5644): Starting input: view=android.widget.ListView@41c2fd98
04-06 11:45:43.456: V/InputMethodManager(5644): Starting input: tba=android.view.inputmethod.EditorInfo@41bf0db8 ic=null
04-06 11:45:43.456: V/InputMethodManager(5644): START INPUT: android.widget.ListView@41c2fd98 ic=null tba=android.view.inputmethod.EditorInfo@41bf0db8 controlFlags=#101
04-06 11:45:43.458: V/InputMethodManager(5644): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@41c0ada8 com.touchtype.swiftkey/com.touchtype.KeyboardService #4808}
04-06 11:45:45.101: D/VelocityTracker(5644): Couldn't open '/dev/touch' (No such file or directory)
04-06 11:45:45.101: D/VelocityTracker(5644): tpd read x fail: Bad file number
04-06 11:45:45.101: D/VelocityTracker(5644): tpd read y fail: Bad file number
04-06 11:45:45.260: I/SurfaceTextureClient(5644): [STC::queueBuffer] (this:0x51247728) fps:1.65, dur:1821.68, max:1796.98, min:9.99
04-06 11:45:46.205: V/Provider/Setting(5644): from settings cache , name = sound_effects_enabled value = 1
04-06 11:45:46.262: I/SurfaceTextureClient(5644): [STC::queueBuffer] (this:0x51247728) fps:3.00, dur:1001.27, max:782.15, min:67.85
04-06 11:45:46.464: D/AbsListView(5644): checkAbsListViewlLogProperty get invalid command
04-06 11:45:46.468: D/dalvikvm(5644): create interp thread : stack size=32KB
04-06 11:45:46.468: D/dalvikvm(5644): create new thread
04-06 11:45:46.469: D/dalvikvm(5644): new thread created
04-06 11:45:46.469: D/dalvikvm(5644): update thread list
04-06 11:45:46.469: D/dalvikvm(5644): threadid=13: interp stack at 0x51cc3000
04-06 11:45:46.469: D/dalvikvm(5644): threadid=13: created from interp
04-06 11:45:46.469: D/dalvikvm(5644): start new thread
04-06 11:45:46.470: D/dalvikvm(5644): threadid=13: notify debugger
04-06 11:45:46.470: D/dalvikvm(5644): threadid=13 (Timer-0): calling run()
04-06 11:45:46.470: D/dalvikvm(5644): threadid=13: exiting
04-06 11:45:46.471: W/dalvikvm(5644): threadid=13: thread exiting with uncaught exception (group=0x40f1e908)
04-06 11:45:46.473: E/AndroidRuntime(5644): FATAL EXCEPTION: Timer-0
04-06 11:45:46.473: E/AndroidRuntime(5644): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.os.Handler.<init>(Handler.java:121)
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.app.Dialog.<init>(Dialog.java:107)
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.app.AlertDialog.<init>(AlertDialog.java:114)
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.app.AlertDialog.<init>(AlertDialog.java:98)
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
04-06 11:45:46.473: E/AndroidRuntime(5644): at com.example.groupchat.MsgViewActivity$GetMsgs.onPreExecute(MsgViewActivity.java:100)
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-06 11:45:46.473: E/AndroidRuntime(5644): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-06 11:45:46.473: E/AndroidRuntime(5644): at com.example.groupchat.MsgViewActivity$1.run(MsgViewActivity.java:57)
04-06 11:45:46.473: E/AndroidRuntime(5644): at java.util.Timer$TimerImpl.run(Timer.java:284)
04-06 11:45:46.482: V/InputMethodManager(5644): focusOut: android.widget.ListView@41c2fd98 mServedView=android.widget.ListView@41c2fd98 winFocus=false
04-06 11:45:46.493: V/InputMethodManager(5644): Not IME target window, ignoring
04-06 11:45:46.675: D/OpenGLRenderer(5644): Flushing caches (mode 0)
04-06 11:45:46.838: D/OpenGLRenderer(5644): Flushing caches (mode 0)
04-06 11:45:48.058: D/OpenGLRenderer(5644): Flushing caches (mode 0)
04-06 11:45:48.071: D/OpenGLRenderer(5644): Flushing caches (mode 0)
04-06 11:45:48.074: D/OpenGLRenderer(5644): Flushing caches (mode 1)
基于android API文档:
无法在计时器上调用AsyncTask.execute。可能您已经这样做了。要以特定的时间间隔运行代码,您可以使用AlarmManager在需要时运行代码。您可以使用
setRepeating()
以指定的间隔运行它
你可以在网上找到很多关于它的帖子
重要的删除代码
newgetmsgs().execute()代码>
从计时器开始,在活动的onCreate方法和活动的onDestroy方法中启动它,停止异步任务
class GetMsgs extends AsyncTask<String, String, String> {
boolean flag = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
flag = true;
}
@Override
protected String doInBackground(String... arg0) {
while(flag)
{
publishProgress("start");
//Your Code what you are doing in the Do inBackground
try
{
Thread.sleep(3000);//Your Interval after which you want to refresh the screen
}
catch (InterruptedException e)
{
}
publishProgress("stop");
}
return null;
}
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
if(values[0].equalsIgnoreCase("start"))
{
//Create Your Dialog in it
//Do here what you are doing in the onPreExecute
}
else if(values[0].equalsIgnoreCase("stop"))
{
//Code you are doing in the onPostExecute
}
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
flag = false;
}
}
类GetMsgs扩展异步任务{
布尔标志=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
flag=true;
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
while(旗帜)
{
出版进度(“开始”);
//你的代码你在后台做什么
尝试
{
Thread.sleep(3000);//您希望刷新屏幕的时间间隔
}
捕捉(中断异常e)
{
}
出版进度(“停止”);
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(字符串…值)
{
super.onProgressUpdate(值);
if(值[0].equalsIgnoreCase(“开始”))
{
//在其中创建对话框
//在这里做你在onPreExecute中正在做的事情
}
else if(值[0].equalsIgnoreCase(“停止”))
{
//在onPostExecute中执行的代码
}
}
@凌驾
受保护的void onCancelled(){
//TODO自动生成的方法存根
super.onCancelled();
flag=false;
}
}
要获得快速响应,您应该发布日志猫跟踪。请发布日志猫以及如何调用任务。您需要从UI线程
Yes中调用AsyncTasks
。那么,如何重复AsyncTask呢?@Priyabrata检查一下,你可以在UI线程的每个地方第一次执行它。接下来,当调用onPostExecute时,您可以在先前运行的AssyncTask中创建一个可运行的并使用Handler.postDelayed(runnable,long)。清楚吗PostDelayed(Runnable r,long)导致将Runnable r添加到消息队列中,并在指定的时间后运行。runnable将在该处理程序所连接的线程上运行。
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
GetMsgs performBackgroundTask = new GetMsgs();
// PerformBackgroundTask this class is the class that extends AsynchTask
performBackgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000*10); //execute in every 10000 ms
}
class GetMsgs extends AsyncTask<String, String, String> {
boolean flag = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
flag = true;
}
@Override
protected String doInBackground(String... arg0) {
while(flag)
{
publishProgress("start");
//Your Code what you are doing in the Do inBackground
try
{
Thread.sleep(3000);//Your Interval after which you want to refresh the screen
}
catch (InterruptedException e)
{
}
publishProgress("stop");
}
return null;
}
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
if(values[0].equalsIgnoreCase("start"))
{
//Create Your Dialog in it
//Do here what you are doing in the onPreExecute
}
else if(values[0].equalsIgnoreCase("stop"))
{
//Code you are doing in the onPostExecute
}
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
flag = false;
}
}