使用后台线程的Android

问题描述:

这是我的服务代码使用后台线程的Android

public class MyService extends Service { 
NotificationManager NM; 
ServerSocket serversocket=null; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("","startcommand is called"); 



    return super.onStartCommand(intent, flags, startId); 
} 


private final IBinder binder=new MyBinder(); 



//this use for binding with activity 
@Override 
public IBinder onBind(Intent intent) { 
    return binder; 
} 



//this class used by the Client to access the service 
public class MyBinder extends Binder { 
     public MyService getService(){ 
      return MyService.this; 
     } 
} 






public void notify(){ 


    Notification notify=new Notification(android.R.drawable.ic_dialog_email,"Service is created",System.currentTimeMillis()); 
    Intent i=new Intent(this, MainActivity.class); 
    i.putExtra("json","the notification method"); 
    PendingIntent pending=PendingIntent.getActivity(
      getApplicationContext(),0, i,0); 
    notify.setLatestEventInfo(this,"subject","body",pending); 
    NM.notify(0, notify); 
} 

@Override 
public void onCreate() { 
    ServerConnection serverConnection=new ServerConnection(); 
    serverConnection.execute(); 

} 





public class ServerConnection extends AsyncTask<Void,String,Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 
     DataInputStream dataInputStream; 

     try { 
      serversocket = new ServerSocket(9100); 
      while(true){ 

       Log.i("aa", "Service is listening to port"); 
       Socket socket = serversocket.accept(); 


      NM = (NotificationManager)  getApplicationContext().getSystemService(NOTIFICATION_SERVICE); 
       publishProgress("ok"); 





      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    @Override 
    protected void onProgressUpdate(String... values) { 
     notify(); 
    } 


    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
    } 





} 

}

我不如果它是确定在服务中使用的AsyncTask但是从发送服务通知反正我每次调用notify()其中发送通知我得到一个错误

java.lang.NullPointerException 
     at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 
     at android.content.ComponentName.<init>(ComponentName.java:75) 
     at android.content.Intent.<init>(Intent.java:3301) 
     at com.example.com.service.MyService.notify(MyService.java:76) 
     at com.example.com.service.MyService$ServerConnection.onProgressUpdate(MyService.java:135) 
     at com.example.com.service.MyService$ServerConnection.onProgressUpdate(MyService.java:95) 
     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4745) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 

它发送,如果把它用在onStartCommand或任何除了在后台线程通知正常工作的通知()函数。

什么,我要做的是每当我收到使用ServerSocket的,我想将通知发送到其不工作的用户的一些数据。

感谢

检查这2线 -

Intent i=new Intent(this, MainActivity.class); 
    //// other lines 
    notify.setLatestEventInfo(this,"subject","body",pending); 

这里,this意味着当你调用从的AsyncTask的方法,权利的AsyncTask?你应该尝试用MyService.this替换它。

+0

THX它的工作还有一个问题我怎么能的情况下,发送消息,从服务活动不会被关闭,这样我不会的情况下,通知发送到应用程序它已经打开 – user2625304 2015-04-06 09:39:23

+1

请不要误会我的意思,你应该为此提出一个单独的问题。在这里分享问题的网址。我们会尽力帮助你解决这个问题。这是必要的,这样下一次有人面临着同样的问题,他们将能够得到答案。 :) – 2015-04-06 09:43:39

+0

确定thx为您的帮助 – user2625304 2015-04-06 09:58:18