使用后台线程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或后台线程以外的任何位置使用了notify()函数,则发送通知的函数都可以正常工作。

我想做的是,每当我使用serversocket接收到一些数据时,我都想向用户发送一个不起作用的通知。

感谢

检查这2行-

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

这里,this的意思是当你从asynctask调用方法时的异步任务,对吧?您应该尝试用MyService.this替换它。

最新更新