繁体   English   中英

显示来自asynctask类的onprogressUpdate方法的通知

[英]displaying notifications from onprogressUpdate method from asynctask class

我正在尝试从asynctack类的on progressupdate方法发送通知。 由于我是android的新手,所以我不知道哪里似乎是错误。 AllSensorData是一个单独的Java类。 如果您能提供帮助,那就太好了。 谢谢

这是我的asyntack类的代码

 public class AllSensorData extends AsyncTask<String, byte[], String>{ TextView temp,humi,motion,smoke,flame,water,reed,data; int notificationID = 1; NotificationManager notificationManager =null; Context context; public AllSensorData(TextView temp,TextView humi,TextView motion,TextView smoke,TextView flame,TextView water,TextView reed) { this.temp=temp; this.humi=humi; this.motion=motion; this.smoke=smoke; this.flame=flame; this.water=water; this.reed=reed; } InputStream nis; OutputStream nos; BufferedReader in; DefaultHttpClient httpclient =new DefaultHttpClient(); URL url; URLConnection urlconn=null; InputStreamReader isn; @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; try { while(true){//while connected HttpGet httpget =new HttpGet("http://192.168.1.177/"); response = httpclient.execute(httpget); in=new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String msgFromServer = in.readLine();//read the lines coming from the socket byte[] theByteArray = msgFromServer.getBytes();//store the bytes in an array publishProgress(theByteArray);//update the publishProgress if(isCancelled()){ break; } } } catch (ClientProtocolException e) { } catch (IOException e) { } return null; } protected void onProgressUpdate(byte[]... values) { super.onProgressUpdate(values); String command=new String(values[0]);//get the String from the recieved bytes String[] parts= command.split(","); String part1=parts[0]; String part2=parts[1]; temp.setText(part1); humi.setText(part2); if(Integer.parseInt(part2)>70) { NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context); builder.setContentTitle("AM Home Automation"); builder.setContentText("humidity > 70"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setTicker("alert"); builder.setDefaults(Notification.DEFAULT_ALL); //builder.setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R)); notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationID, builder.build()); notificationID++; } } } 

当我浏览代码时,尽管上下文已在类中声明,但我认为上下文并未正确初始化。 您可以在类中的任何地方使用它之前交叉检查它是否已初始化(特别是在通知生成器中)

尝试在课堂上进行以下更改

public AllSensorData(TextView temp,TextView humi,TextView motion,TextView smoke,TextView flame,TextView water,TextView reed, Context context) {
                this.temp=temp;
                this.humi=humi;
                this.motion=motion;
                this.smoke=smoke;
                this.flame=flame;
                this.water=water;
                this.reed=reed;
                this.context=context
            }

另外,请记住在初始化此类时从主活动中传递应用程序上下文,如下所示:

AllSensorData mSensorData = new AllSensorData(temp, humi, motion, smoke, flame, water,reed,getApplicationContext());

那可能只是解决您的问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM