Hitmanpt Posted April 15, 2012 at 01:03 AM Report #449242 Posted April 15, 2012 at 01:03 AM Boas pessoal o meu problema é o seguinte eu comecei recentemente a programar para android e agr num dos programas que estou a a fazer decidi usar multithreading Já fiz varios funcionar mas bolqueei nesta parte então é assim tenho um servico no ficheiro/classe service.java dps noutros ficheiros tenho os threads thread1.java, thread2.java (não com estes nomes como é obvio até agr consegui esta ligação service -> thread1 a funcionar corretamente (o thread 1 é simples portanto nao tive problemas) á medida que fui programando tive que comecar outra parte em que tem que ser no thread2.java este tem mais problemas a estrutura é service -> thread1 -> thread2 e aqui está o problema no arranque do servico foi defenido uma notificação "OnGoing" logo nunca desapareçe até eu dizer, mas o thread2 vai ter que a atualizar e é ai que reside o problema package com.pcdev.pcnotify; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.util.EventObject; import android.app.Activity; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; public class thread2 implements Runnable{ public void run() { while (1==1){ String connectcmd = "ispcUP?"; Socket socket = null; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; try { socket = new Socket(PCNotifyService.connectionIP, Integer.parseInt(PCNotifyService.connectionPort)); dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataInputStream = new DataInputStream(socket.getInputStream()); dataOutputStream.write(connectcmd.getBytes()); /** textIn.setText(dataInputStream.readUTF()); */ } catch (UnknownHostException e) { PCNotifyService.Srvhandler.post(new Runnable() { public void run() { Intent intent = new Intent(this, Activity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); int icon = R.drawable.ic_launcher; CharSequence text = "PCNotify Service Started"; CharSequence contentTitle = "PCNotify - Connected"; CharSequence contentText = "Computer IP: " + service.connIP + ":" + service.connPort; long when = System.currentTimeMillis(); Notification notification = new Notification(icon,text,when); notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); service.notificationManager.notify(0, notification); }}); } catch (IOException e) { } finally{ if (socket != null) { try { socket.close(); } catch (IOException e) {} if (dataOutputStream != null){ try { dataOutputStream.close(); } catch (IOException e) {} } if (dataInputStream != null){ try { dataInputStream.close(); } catch (IOException e) {} }; } try { Thread.sleep(5000); } catch (InterruptedException e) {e.printStackTrace();}} } } Estou com problemas nestas linhas Intent intent = new Intent(this, PCNotifyActivity.class); <erro Description The constructor Intent(new Runnable(){}, Class<Activity>) is undefined thread2.java> PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); <erro Description The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (new Runnable(){}, int, Intent, int) thread2.java> notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); <erro Description The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) in the type Notification is not applicable for the arguments (new Runnable(){}, CharSequence, CharSequence, PendingIntent) thread2.java> Se bem que acho que o 3º erro é derivado dos anteriores.... Alguma ajuda?
KTachyon Posted April 15, 2012 at 01:47 AM Report #449246 Posted April 15, 2012 at 01:47 AM Pois... basicamente tu estás a passar qualquer coisa (neste caso, um Runnable) que não é uma Activity para o contrutor do Intent, logo ele não consegue obter o Context (do qual a classe Activity é uma subclasse de uma subclasse). Ou seja, de alguma forma terás que passar a Activity para o Runnable, de forma a que possas passar o Context para o Intent. Agora lê esta frase 3 vezes, muito depressa 😕 A mesma coisa para o PendingIntent e para o setLatestEventInfo(). “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
Hitmanpt Posted April 15, 2012 at 08:58 AM Author Report #449256 Posted April 15, 2012 at 08:58 AM Ok... eu adicionei á class "thread2" extends Activity mas msm assim não funciona :S não sei o que estou a fazer mal Alguem me pode mostrar um exemplo... É que sou um bocado novo no Java
KTachyon Posted April 15, 2012 at 01:55 PM Report #449289 Posted April 15, 2012 at 01:55 PM Não podes extender a classe como Activity se tem que ser um Runnable. Podes é passar a Activity para o Runnable para que tenhas acesso a ela no run(). Ou isso, ou fazes uma função anónima dentro da Activity: public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Runnable myRunnable = new Runnable() { @Override public void run() { //... coisas... entre elas, para aceder à activity: MyActivity.this } } Thread thread = new Thread(null, myRunnable, "myThread"); thread.start(); } } A primeira solução é tão simples como criar um construtor na tua classe actual que receba uma referência para a Activity que a chama. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
Hitmanpt Posted April 15, 2012 at 10:07 PM Author Report #449361 Posted April 15, 2012 at 10:07 PM Ok obrigado pela a ajuda 😕
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now