0

這是我的服務代碼使用後臺線程的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

檢查這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