Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerService extends Service {
- public static Context context;
- boolean isPlay = false;
- String stream;
- String radio;
- Notification notification;
- Notification status;
- public static final String KEY_STREAM = PlayerService.class.getSimpleName() + ".KEY_STREAM";
- public static final String KEY_RADIO = PlayerService.class.getSimpleName() + ".KEY_RADIO";
- final String myLog = "LOG";
- private void showNotification() {
- Intent notificationIntent = new Intent(this, MainActivity.class);
- notificationIntent.setAction(PlayerConstants.ACTION.MAIN_ACTION);
- notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- Intent closeIntent = new Intent(this, PlayerService.class);
- closeIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
- PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0);
- notification = new NotificationCompat.Builder(this)
- .setContentTitle("Играет: " + radio)
- .setContentText("Открыть список радиостанций")
- .setSmallIcon(R.mipmap.ic_launcher)
- .addAction(R.drawable.icon_stop, "Стоп", pCloseIntent)
- .setContentIntent(pendingIntent).build();
- startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
- }
- private void showNotificationOld() {
- Intent notificationIntent = new Intent(this, MainActivity.class);
- notificationIntent.setAction(PlayerConstants.ACTION.MAIN_ACTION);
- notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
- Intent closeIntent = new Intent(this, PlayerService.class);
- closeIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
- PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0);
- notification = new NotificationCompat.Builder(this)
- .setContentTitle("Играет: " + radio)
- .setContentText("Открыть список радиостанций")
- .setSmallIcon(R.mipmap.ic_launcher)
- .setDeleteIntent(pCloseIntent)
- .setContentIntent(pendingIntent).build();
- startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
- }
- @Override
- public void onCreate() {
- super.onCreate();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- context = this;
- this.stream = intent.getStringExtra(KEY_STREAM);
- this.radio = intent.getStringExtra(KEY_RADIO);
- if (intent.getAction() != null && intent.getAction().equals(PlayerConstants.ACTION.STARTFOREGROUND_ACTION)) {
- isPlay = true;
- if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
- showNotification();
- } else {
- showNotificationOld();
- }
- RadioPlayer.startPlayer(stream, this);
- } else if (intent.getAction() != null && intent.getAction().equals(PlayerConstants.ACTION.STOPFOREGROUND_ACTION)) {
- isPlay = false;
- RadioPlayer.stopPlayer();
- stopForeground(true);
- Player.btnPLayPause.setImageResource(R.drawable.icon_play);
- Player.isPlay = false;
- stopSelf();
- }
- return START_REDELIVER_INTENT;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- RadioPlayer.stopPlayer();
- stopForeground(true);
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- }
- public class Player extends AppCompatActivity
- implements NavigationView.OnNavigationItemSelectedListener {
- private String stream;
- private String radio;
- private String img;
- private int radioID;
- DBHeler db;
- private String MAYBE_ACTION = "MAYBE_ACTION";
- static boolean isPlay = false;
- static ImageButton btnPLayPause;
- private TextView txtRadio;
- private TextView txtTitle;
- private RoundedImageView imgRadio;
- public static String LOG_TAG = "my_log";
- private String internetStatus = "";
- BroadcastReceiver br;
- private AsyncTask jsonTask;
- private ScheduledFuture<?> mScheduledFuture;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_player);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.setDrawerListener(toggle);
- toggle.syncState();
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- navigationView.setNavigationItemSelectedListener(this);
- db = new DBHeler(this);
- radioID = getIntent().getExtras().getInt("id");
- String query = "SELECT * " + " FROM " + Contract.Entry.TABLE_RADIO + " WHERE " + Contract.Entry._ID + " = " + radioID;
- Cursor cursor = db.database.rawQuery(query, null);
- try {
- if (cursor.getCount() != 0) {
- while (cursor.moveToNext()) {
- radio = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_TITLE));
- stream = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_URL));
- img = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_IMAGE));
- }
- }
- } catch (SQLException ex) {
- } finally {
- cursor.close();
- }
- btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause);
- txtRadio = (TextView) findViewById(R.id.txtRadio);
- txtTitle = (TextView) findViewById(R.id.txtTitle);
- imgRadio = (RoundedImageView) findViewById(R.id.imgRadio);
- setTitle(radio);
- txtRadio.setText(radio);
- try {
- InputStream ims = getAssets().open("img/" + img + ".png");
- Drawable d = Drawable.createFromStream(ims, null);
- imgRadio.setImageDrawable(d);
- } catch(IOException ex) {
- return;
- }
- if (NetworkUtil.getConnectivityStatus(this) != 0) {
- startPlayerService();
- }
- br = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- finish();
- }
- };
- IntentFilter intentFilter = new IntentFilter(MAYBE_ACTION);
- registerReceiver(br, intentFilter);
- }
- @Override
- protected void onStart() {
- super.onStart();
- mScheduledFuture = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
- @Override
- public void run() {
- jsonTask = new ParseJSONTask().execute();
- }
- }, 0, 2, TimeUnit.SECONDS);
- }
- public void startPlayerService() {
- Intent serviceIntent = new Intent(Player.this, PlayerService.class);
- serviceIntent.putExtra(PlayerService.KEY_STREAM, stream);
- serviceIntent.putExtra(PlayerService.KEY_RADIO, radio);
- serviceIntent.setAction(PlayerConstants.ACTION.STARTFOREGROUND_ACTION);
- startService(serviceIntent);
- isPlay = true;
- btnPLayPause.setImageResource(R.drawable.icon_stop);
- mScheduledFuture = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
- @Override
- public void run() {
- jsonTask = new ParseJSONTask().execute();
- }
- }, 0, 2, TimeUnit.SECONDS);
- }
- private void stopPlayerService() {
- Intent serviceIntent = new Intent(Player.this, PlayerService.class);
- serviceIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
- stopService(serviceIntent);
- isPlay = false;
- btnPLayPause.setImageResource(R.drawable.icon_play);
- txtTitle.setText("");
- mScheduledFuture.cancel(false);
- }
- private BroadcastReceiver NetworkChangeReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- internetStatus = NetworkUtil.getConnectivityStatusString(context);
- Toast.makeText(context, internetStatus, Toast.LENGTH_LONG).show();
- }
- };
- @Override
- protected void onResume() {
- super.onResume();
- registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
- registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED"));
- }
- @Override
- protected void onStop() {
- super.onStop();
- mScheduledFuture.cancel(false);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (NetworkChangeReceiver != null) unregisterReceiver(NetworkChangeReceiver);
- unregisterReceiver(br);
- }
- public void onPlayPause(View view) {
- if (NetworkUtil.getConnectivityStatus(this) != 0 ) {
- if (isPlay) {
- stopPlayerService();
- } else {
- startPlayerService();
- }
- } else {
- Toast.makeText(this, "Нет подключения к интернету", Toast.LENGTH_LONG).show();
- stopPlayerService();
- }
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }
- }
- private class ParseJSONTask extends AsyncTask<Void, Void, String> {
- HttpURLConnection urlConnection = null;
- BufferedReader reader = null;
- String resultJson = "";
- @Override
- protected String doInBackground(Void... params) {
- try {
- URL url = new URL("URL");
- urlConnection = (HttpURLConnection) url.openConnection();
- urlConnection.setRequestMethod("GET");
- urlConnection.connect();
- InputStream inputStream = urlConnection.getInputStream();
- StringBuffer buffer = new StringBuffer();
- reader = new BufferedReader(new InputStreamReader(inputStream));
- String line;
- while ((line = reader.readLine()) != null) {
- buffer.append(line);
- }
- resultJson = buffer.toString();
- }catch (Exception e) {
- e.printStackTrace();
- }
- return resultJson;
- }
- @Override
- protected void onCancelled() {
- super.onCancelled();
- }
- @Override
- protected void onPostExecute(String strJson) {
- super.onPostExecute(strJson);
- JSONObject dataJsonObj = null;
- String replaceStr = "(***.ru)";
- try {
- dataJsonObj = new JSONObject(strJson);
- JSONArray radioArray = dataJsonObj.getJSONArray("stations");
- for (int i = 0; i < radioArray.length(); i++) {
- JSONObject radioObject = radioArray.getJSONObject(i);
- String radioUrl = radioObject.optString("url");
- String title = radioObject.optString("title");
- Log.d(LOG_TAG, radioUrl);
- Log.d(LOG_TAG, title);
- String radioTitle = "";
- try {
- radioTitle = new String(title.getBytes("Cp1252"),"Cp1251");
- radioTitle = new String(radioTitle.getBytes(),"utf-8");
- radioTitle = radioTitle.toLowerCase();
- if (radioTitle.contains(replaceStr)) {
- radioTitle = radioTitle.replace(replaceStr, "");
- }
- } catch (UnsupportedEncodingException ex) {
- ex.printStackTrace();
- }
- if (isPlay) {
- if (radioUrl.equals(stream)) {
- txtTitle.setText(radioTitle.toUpperCase());
- }
- } else {
- txtTitle.setText("");
- }
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- }
- java.lang.RuntimeException:
- at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3045)
- at android.app.ActivityThread.access$2200 (ActivityThread.java:157)
- at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1454)
- at android.os.Handler.dispatchMessage (Handler.java:102)
- at android.os.Looper.loop (Looper.java:148)
- at android.app.ActivityThread.main (ActivityThread.java:5525)
- at java.lang.reflect.Method.invoke (Native Method)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
- at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)
- Caused by: java.lang.NullPointerException:
- at ru.umagadzhi.caucasusradios.PlayerService.onStartCommand (PlayerService.java:99)
- at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3028)
- Player.btnPLayPause.setImageResource(R.drawable.icon_play);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement