如何修复错误“不幸的是,进程com.android.systemui已停止”

时间:2015-06-04 17:48:34

标签: android android-mediaplayer genymotion

我正在开发一个流式传输应用,可以播放调频广播电台的互联网信息。

当应用程序加载,然后我点击播放按钮时,我的模拟器崩溃,我收到以下错误:“不幸的是,进程com.android.systemui已经停止”模拟器返回到锁定屏幕,但是流无论如何开始播放,然后当我解锁手机屏幕时,应用程序恢复正常。

我认为这可能是我的模拟器的一个问题,但我不确定。我正在使用Genymotion android模拟器。我的代码如下:

public class MainActivity extends Activity {


private boolean initialState = true;
private ImageButton button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (ImageButton)findViewById(R.id.btnPlay);

    button.setBackgroundColor(Color.BLACK);

    button.setImageDrawable(scaleImage(ContextCompat.getDrawable(this, R.drawable.playwhite),0.2f));
}

public void playPause(View arg0) {
   // Intent startIntent = new Intent(MainActivity.this,ForegroundService.class);
   // startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
   // startService(startIntent);

    if (initialState == true) {
        // play
        button.setBackgroundColor(Color.BLACK);
        button.setImageDrawable(scaleImage(ContextCompat.getDrawable(this, R.drawable.playwhite), 0.2f));

        Intent startIntent = new Intent(MainActivity.this, MusicService.class);

        startIntent.setAction(Constants.ACTION.PLAYMUSIC_ACTION);
        startService(startIntent);
        initialState = false;
        System.out.println("Play: " + startIntent.getAction());
    } else {
        // pause
        button.setBackgroundColor(Color.BLACK);
        button.setImageDrawable(scaleImage(ContextCompat.getDrawable(this, R.drawable.pausewhite), 0.2f));
        Intent startIntent = new Intent(MainActivity.this, MusicService.class);
        startIntent.setAction(Constants.ACTION.PAUSEMUSIC_ACTION);
        startService(startIntent);
        initialState = true;
        System.out.println("Pause: " + startIntent.getAction());
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


public Drawable scaleImage (Drawable image, float scaleFactor) {

    if ((image == null) || !(image instanceof BitmapDrawable)) {
        return image;
    }

    Bitmap b = ((BitmapDrawable)image).getBitmap();

    int sizeX = Math.round(image.getIntrinsicWidth() * scaleFactor);
    int sizeY = Math.round(image.getIntrinsicHeight() * scaleFactor);

    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, sizeX, sizeY, false);

    image = new BitmapDrawable(getResources(), bitmapResized);

    return image;

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

我的MusicService课程:

public class MusicService extends Service {

private MediaPlayer mediaPlayer;
WifiManager.WifiLock wifiLock;
private String streamUrl = "http://amber.streamguys...." // station stream url;

public MusicService() {}

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(Constants.ACTION.PLAYMUSIC_ACTION)) {
        play();
    }
    else if (intent.getAction().equals(Constants.ACTION.PAUSEMUSIC_ACTION)) {
        pause();
    }
    return super.onStartCommand(intent, flags, startId);

}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void play() {
    if(mediaPlayer==null) {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }
    try {getSystemService(Context.WIFI_SERVICE))
        mediaPlayer.setDataSource(streamUrl);
        mediaPlayer.prepareAsync();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            // play when enough information has been buffered
            mediaPlayer.start();
        }
    });


    Notification note=new Notification(R.drawable.infidel,
            "This is where notifcation goes",
            System.currentTimeMillis());
    Intent i=new Intent(this, MainActivity.class);

    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
            Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi=PendingIntent.getActivity(this, 0,
            i, 0);

    note.setLatestEventInfo(this, "Test",
            "Now Playing: \"Nothing yet\"",
            pi);

    note.flags|=Notification.FLAG_NO_CLEAR;

    startForeground(1312, note);
}

private void pause() {
    if (mediaPlayer.isPlaying()) {
        try {
            mediaPlayer.stop();
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }catch (IllegalStateException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    stopForeground(true);
}

}

通过初始构建的应用程序进行Stacktrace,并在播放和暂停之间进行几次单击切换:

I/System.out﹕ Play: com.example...action.playaction
E/MediaPlayer﹕ Should have subtitle controller already set
D/dalvikvm﹕ GC_FOR_ALLOC freed 2415K, 43% free 3669K/6436K, paused 2ms, total 2ms
I/dalvikvm-heap﹕ Grow heap (frag case) to 5.988MB for 2359308-byte allocation
D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 8% free 5972K/6436K, paused 2ms, total 2ms
I/System.out﹕ Pause: com.example...action.pauseaction
D/dalvikvm﹕ GC_FOR_ALLOC freed 2398K, 43% free 3669K/6436K, paused 3ms, total 3ms 
I/dalvikvm-heap﹕ Grow heap (frag case) to 5.988MB for 2359308-byte allocation 
D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 8% free 5971K/6436K, paused 2ms, total 2ms
I/System.out﹕ Play: com.example...action.playaction
E/MediaPlayer﹕ Should have subtitle controller already set
D/dalvikvm﹕ GC_FOR_ALLOC freed 2405K, 43% free 3669K/6436K, paused 7ms, total 7ms
I/dalvikvm-heap﹕ Grow heap (frag case) to 5.988MB for 2359308-byte allocation
D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 8% free 5972K/6436K, paused 3ms, total 4ms
I/System.out﹕ Pause: com.example...action.pauseaction

1 个答案:

答案 0 :(得分:0)

服务在主线程上运行,在后台线程上运行服务

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                if (intent.getAction().equals(Constants.ACTION.PLAYMUSIC_ACTION)) {
                    play();
                } else if (intent.getAction().equals(Constants.ACTION.PAUSEMUSIC_ACTION)) {
                    pause();
                }
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }