Pergunta

I'm trying to send a message from the Service and I receive an error

Error at com.spynetstation.MediaService.notif(MediaService.java:162)

which points to this line in the Service:

Intent notificationIntent = new Intent(ctx, MediaService.class);

Service:

public class MediaService extends Service implements OnPreparedListener,OnCompletionListener{

    static MediaPlayer mediaPlayer;
    static NotificationManager nm;
    private static NotificationCompat.Builder mBuilder;
    static Context ctx;

    static String dataPlaying = null;
    static String soursetrack = null;
    static TimerTask repeatTask;
    static Timer t;
    Node node;
    static String filePath;
    static File file;
    String titleNotif = "Station";
    String contentNotif = "Running";

    public IBinder onBind(Intent paramIntent)  {
        return null;
    }

    public static void initMP(String urlStream){
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(urlStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.prepareAsync();
        Log.d("MediaService", "prepareAsync");
    }

    public static void startMP(){
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
            @Override  
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                Log.d("MediaService", "start");
                MainActivity.visible();
                if (MainActivity.track) {} else {doRepeatTask();}
            }
        });   
    }

    public static void stopMP() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.stop(); 
                Log.d("MediaService", "stop");
                if (MainActivity.track) {} else {stopRepeatTask();}
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void releaseMP() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.release();
                Log.d("MediaService", "release"); 
                mediaPlayer = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d("MediaService", "onCompletion"); 
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d("MediaService", "onPrepareed");
    }

    public void onCreate()  {
        super.onCreate();
        // tried the following ctx=getBaseContext(); did not help
        ctx = getApplicationContext();
        this.nm = ((NotificationManager)getSystemService("notification"));
    }

    public void onDestroy()  {
        this.nm.cancelAll();
        stopForeground(true);
    }

    public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)  {
        try {
            TimeUnit.SECONDS.sleep(0);
            notif(titleNotif, contentNotif);
            return super.onStartCommand(paramIntent, paramInt1, paramInt2);
        } catch (InterruptedException localInterruptedException) {
            for (;;) {
                localInterruptedException.printStackTrace();
            }
        }
    }

    public static void notif(String titleNotif, String contentNotif){
        //building the notification
        mBuilder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(R.drawable.spy)
            .setContentTitle(titleNotif)
            .setTicker(contentNotif);

        Intent notificationIntent = new Intent(ctx, MediaService.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, 
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        Notification n = mBuilder.build();
        nm.notify(1, n);
        // startForeground(12345, mBuilder.build());
    }

    public static void doRepeatTask(){
        t = new Timer();
        repeatTask = new TimerTask() {
            public void run() {
                new Task().execute();
            }
        };
        t.schedule(repeatTask, 300, 3000); 
    }

    public static void stopRepeatTask(){
        if(repeatTask!=null){
            repeatTask.cancel();
            Log.d("TIMER", "timer canceled");
        }
        MainActivity.titleMusic.setText("");
        MainActivity.titleMusic.setVisibility(View.GONE);
    }

    public static class Task extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                URL url = new URL(soursetrack);
                URLConnection connection = url.openConnection();
                InputStream in = connection.getInputStream();
                filePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/cashe.xml";
                file = new File (filePath);
                CreateFileFromInputStream(in,  filePath) ;
                // Parse it with document builder factory
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = null;
                dBuilder = dbFactory.newDocumentBuilder();
                Document doc = null;
                doc = dBuilder.parse(file);
                doc.getDocumentElement().normalize();
                // The root element is 
                doc.getDocumentElement().getNodeName();
                NodeList nList = doc.getElementsByTagName("Name");

                for ( int i = 0 ; i < nList.getLength() ; i++ ) {
                    Element element = (Element) nList.item(i) ;
                    dataPlaying = getCharacterDataFromElement(element);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace(); 
            } catch (ParserConfigurationException e) {
                e.printStackTrace(); 
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace(); 
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (dataPlaying.equals(MainActivity.currentlyPlaying)){
                if (MainActivity.replay == true){
                    MainActivity.replay = false;
                    MainActivity.titleMusic.setText("" + MainActivity.currentlyPlaying); 
                }
            } else {
                Log.d("MainActivity","Change track");
                MainActivity.currentlyPlaying = dataPlaying;
                MainActivity.titleMusic.setText("" + MainActivity.currentlyPlaying);
                send();
            }
            super.onPostExecute(result);  
        }
    }

    public static void send(){
        notif(MainActivity.currentlyPlaying,MainActivity.currentlyPlaying);
    }

    public static String getCharacterDataFromElement(Element e) {
        Node node = e.getFirstChild();
        if (node instanceof CharacterData) {
            CharacterData cd = (CharacterData) node;
            return cd.getData();
        }
        return "";
    }

    public static void CreateFileFromInputStream(InputStream inStream, String path)
            throws IOException {
        // write the inputStream to a FileOutputStream
        OutputStream out = new FileOutputStream(new File(path));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }

        inStream.close();
        out.flush();
        out.close();
    }
}

LogCat:

03-12 06:16:21.324: D/MediaService(3475): Change track
03-12 06:16:21.347: D/AndroidRuntime(3475): Shutting down VM
03-12 06:16:21.347: W/dalvikvm(3475): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-12 06:16:21.394: E/AndroidRuntime(3475): FATAL EXCEPTION: main
03-12 06:16:21.394: E/AndroidRuntime(3475): java.lang.NullPointerException
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.content.ComponentName.<init>(ComponentName.java:75)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.content.Intent.<init>(Intent.java:3122)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService.notif(MediaService.java:162)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService.send(MediaService.java:250)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService$Task.onPostExecute(MediaService.java:241)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.spynetstation.MediaService$Task.onPostExecute(MediaService.java:1)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.AsyncTask.finish(AsyncTask.java:602)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.os.Looper.loop(Looper.java:137)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-12 06:16:21.394: E/AndroidRuntime(3475):     at dalvik.system.NativeStart.main(Native Method)
Foi útil?

Solução

I think its the context.

Instead of ctx=this,

You can use getApplicationContext() or getBaseContext() inside your service to get the context.

Try using

ctx= getApplicationContext();

OR

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

Refer: android start activity from service

Outras dicas

Replace

 Intent notificationIntent = new Intent(ctx, MainActivity.class);

With

 Intent notificationIntent = new Intent(getBaseContext(), MainActivity.class);

Or

 Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

Because, Intent.FLAG_ACTIVITY_SINGLE_TOP flag is not support this.
So you can try getApplicationContext() or getBaseContext() instead ctx.
You can read this. It can be help you. android start activity from service

You're passing a null context variable in ctx. Also, your architecture is just wierd- notif should not be a static member on the service. It should be a non-static normal member function. You should never have a static reference to a Context anywhere- iots a leak. And its not needed since a service is a context, it should be able to pass in this if notif wasn't static. Anytime you find yourself putting multiple static variables in a class like that it's a sign that you've designed things wrong.

Use:-

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

You're passing a null context variable in ctx. well i have solution and it works well you have to create a static context in your main class and then use that context

public class LauncherActivityname extends ..... {

public static Context context;
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this
}
}

use this context insted of yout ctx and use it like

Intent notificationIntent = new Intent(LauncherActivityname.context, MainActivity.class);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top