Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.68 KB | None | 0 0
  1. public class PlayerService extends Service {
  2. public static Context context;
  3. boolean isPlay = false;
  4. String stream;
  5. String radio;
  6. Notification notification;
  7. Notification status;
  8.  
  9. public static final String KEY_STREAM = PlayerService.class.getSimpleName() + ".KEY_STREAM";
  10. public static final String KEY_RADIO = PlayerService.class.getSimpleName() + ".KEY_RADIO";
  11.  
  12. final String myLog = "LOG";
  13.  
  14. private void showNotification() {
  15. Intent notificationIntent = new Intent(this, MainActivity.class);
  16. notificationIntent.setAction(PlayerConstants.ACTION.MAIN_ACTION);
  17. notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  18.  
  19. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  20.  
  21. Intent closeIntent = new Intent(this, PlayerService.class);
  22. closeIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
  23. PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0);
  24.  
  25. notification = new NotificationCompat.Builder(this)
  26. .setContentTitle("Играет: " + radio)
  27. .setContentText("Открыть список радиостанций")
  28. .setSmallIcon(R.mipmap.ic_launcher)
  29. .addAction(R.drawable.icon_stop, "Стоп", pCloseIntent)
  30. .setContentIntent(pendingIntent).build();
  31. startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
  32. }
  33.  
  34. private void showNotificationOld() {
  35. Intent notificationIntent = new Intent(this, MainActivity.class);
  36. notificationIntent.setAction(PlayerConstants.ACTION.MAIN_ACTION);
  37. notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  38.  
  39. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  40.  
  41. Intent closeIntent = new Intent(this, PlayerService.class);
  42. closeIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
  43. PendingIntent pCloseIntent = PendingIntent.getService(this, 0, closeIntent, 0);
  44.  
  45. notification = new NotificationCompat.Builder(this)
  46. .setContentTitle("Играет: " + radio)
  47. .setContentText("Открыть список радиостанций")
  48. .setSmallIcon(R.mipmap.ic_launcher)
  49. .setDeleteIntent(pCloseIntent)
  50. .setContentIntent(pendingIntent).build();
  51. startForeground(PlayerConstants.NOTIFICATION_ID.PLAYER_SERVICE_ID, notification);
  52. }
  53.  
  54. @Override
  55. public void onCreate() {
  56. super.onCreate();
  57. }
  58.  
  59. @Override
  60. public int onStartCommand(Intent intent, int flags, int startId) {
  61. context = this;
  62. this.stream = intent.getStringExtra(KEY_STREAM);
  63. this.radio = intent.getStringExtra(KEY_RADIO);
  64.  
  65. if (intent.getAction() != null && intent.getAction().equals(PlayerConstants.ACTION.STARTFOREGROUND_ACTION)) {
  66. isPlay = true;
  67. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
  68. showNotification();
  69. } else {
  70. showNotificationOld();
  71. }
  72. RadioPlayer.startPlayer(stream, this);
  73.  
  74. } else if (intent.getAction() != null && intent.getAction().equals(PlayerConstants.ACTION.STOPFOREGROUND_ACTION)) {
  75. isPlay = false;
  76. RadioPlayer.stopPlayer();
  77. stopForeground(true);
  78. Player.btnPLayPause.setImageResource(R.drawable.icon_play);
  79. Player.isPlay = false;
  80. stopSelf();
  81. }
  82.  
  83. return START_REDELIVER_INTENT;
  84. }
  85.  
  86. @Override
  87. public void onDestroy() {
  88. super.onDestroy();
  89. RadioPlayer.stopPlayer();
  90. stopForeground(true);
  91. }
  92.  
  93. @Nullable
  94. @Override
  95. public IBinder onBind(Intent intent) {
  96. return null;
  97. }
  98. }
  99.  
  100. public class Player extends AppCompatActivity
  101. implements NavigationView.OnNavigationItemSelectedListener {
  102.  
  103. private String stream;
  104. private String radio;
  105. private String img;
  106. private int radioID;
  107.  
  108. DBHeler db;
  109.  
  110. private String MAYBE_ACTION = "MAYBE_ACTION";
  111. static boolean isPlay = false;
  112. static ImageButton btnPLayPause;
  113. private TextView txtRadio;
  114. private TextView txtTitle;
  115. private RoundedImageView imgRadio;
  116.  
  117. public static String LOG_TAG = "my_log";
  118. private String internetStatus = "";
  119.  
  120. BroadcastReceiver br;
  121. private AsyncTask jsonTask;
  122.  
  123. private ScheduledFuture<?> mScheduledFuture;
  124.  
  125. @Override
  126. protected void onCreate(Bundle savedInstanceState) {
  127. super.onCreate(savedInstanceState);
  128. setContentView(R.layout.activity_player);
  129. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  130. setSupportActionBar(toolbar);
  131.  
  132. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  133. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  134. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  135. drawer.setDrawerListener(toggle);
  136. toggle.syncState();
  137.  
  138. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  139. navigationView.setNavigationItemSelectedListener(this);
  140.  
  141. db = new DBHeler(this);
  142.  
  143. radioID = getIntent().getExtras().getInt("id");
  144.  
  145. String query = "SELECT * " + " FROM " + Contract.Entry.TABLE_RADIO + " WHERE " + Contract.Entry._ID + " = " + radioID;
  146. Cursor cursor = db.database.rawQuery(query, null);
  147. try {
  148. if (cursor.getCount() != 0) {
  149. while (cursor.moveToNext()) {
  150. radio = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_TITLE));
  151. stream = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_URL));
  152. img = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_IMAGE));
  153. }
  154. }
  155. } catch (SQLException ex) {
  156. } finally {
  157. cursor.close();
  158. }
  159.  
  160. btnPLayPause = (ImageButton) findViewById(R.id.btnPLayPause);
  161. txtRadio = (TextView) findViewById(R.id.txtRadio);
  162. txtTitle = (TextView) findViewById(R.id.txtTitle);
  163. imgRadio = (RoundedImageView) findViewById(R.id.imgRadio);
  164.  
  165. setTitle(radio);
  166. txtRadio.setText(radio);
  167.  
  168. try {
  169. InputStream ims = getAssets().open("img/" + img + ".png");
  170. Drawable d = Drawable.createFromStream(ims, null);
  171. imgRadio.setImageDrawable(d);
  172. } catch(IOException ex) {
  173. return;
  174. }
  175.  
  176. if (NetworkUtil.getConnectivityStatus(this) != 0) {
  177. startPlayerService();
  178. }
  179.  
  180. br = new BroadcastReceiver() {
  181. @Override
  182. public void onReceive(Context context, Intent intent) {
  183. finish();
  184. }
  185. };
  186. IntentFilter intentFilter = new IntentFilter(MAYBE_ACTION);
  187. registerReceiver(br, intentFilter);
  188. }
  189.  
  190. @Override
  191. protected void onStart() {
  192. super.onStart();
  193. mScheduledFuture = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
  194. @Override
  195. public void run() {
  196. jsonTask = new ParseJSONTask().execute();
  197. }
  198. }, 0, 2, TimeUnit.SECONDS);
  199. }
  200.  
  201. public void startPlayerService() {
  202. Intent serviceIntent = new Intent(Player.this, PlayerService.class);
  203. serviceIntent.putExtra(PlayerService.KEY_STREAM, stream);
  204. serviceIntent.putExtra(PlayerService.KEY_RADIO, radio);
  205. serviceIntent.setAction(PlayerConstants.ACTION.STARTFOREGROUND_ACTION);
  206. startService(serviceIntent);
  207. isPlay = true;
  208. btnPLayPause.setImageResource(R.drawable.icon_stop);
  209. mScheduledFuture = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
  210. @Override
  211. public void run() {
  212. jsonTask = new ParseJSONTask().execute();
  213. }
  214. }, 0, 2, TimeUnit.SECONDS);
  215. }
  216.  
  217. private void stopPlayerService() {
  218. Intent serviceIntent = new Intent(Player.this, PlayerService.class);
  219. serviceIntent.setAction(PlayerConstants.ACTION.STOPFOREGROUND_ACTION);
  220. stopService(serviceIntent);
  221. isPlay = false;
  222. btnPLayPause.setImageResource(R.drawable.icon_play);
  223. txtTitle.setText("");
  224. mScheduledFuture.cancel(false);
  225. }
  226.  
  227. private BroadcastReceiver NetworkChangeReceiver = new BroadcastReceiver() {
  228. @Override
  229. public void onReceive(Context context, Intent intent) {
  230. internetStatus = NetworkUtil.getConnectivityStatusString(context);
  231. Toast.makeText(context, internetStatus, Toast.LENGTH_LONG).show();
  232. }
  233. };
  234.  
  235. @Override
  236. protected void onResume() {
  237. super.onResume();
  238. registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
  239. registerReceiver(NetworkChangeReceiver, new IntentFilter("android.net.wifi.WIFI_STATE_CHANGED"));
  240. }
  241.  
  242. @Override
  243. protected void onStop() {
  244. super.onStop();
  245. mScheduledFuture.cancel(false);
  246. }
  247.  
  248. @Override
  249. protected void onDestroy() {
  250. super.onDestroy();
  251. if (NetworkChangeReceiver != null) unregisterReceiver(NetworkChangeReceiver);
  252. unregisterReceiver(br);
  253. }
  254.  
  255. public void onPlayPause(View view) {
  256. if (NetworkUtil.getConnectivityStatus(this) != 0 ) {
  257. if (isPlay) {
  258. stopPlayerService();
  259. } else {
  260. startPlayerService();
  261. }
  262. } else {
  263. Toast.makeText(this, "Нет подключения к интернету", Toast.LENGTH_LONG).show();
  264. stopPlayerService();
  265. }
  266. }
  267.  
  268. @Override
  269. public void onBackPressed() {
  270. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  271. if (drawer.isDrawerOpen(GravityCompat.START)) {
  272. drawer.closeDrawer(GravityCompat.START);
  273. } else {
  274. super.onBackPressed();
  275. }
  276. }
  277.  
  278. private class ParseJSONTask extends AsyncTask<Void, Void, String> {
  279. HttpURLConnection urlConnection = null;
  280.  
  281. BufferedReader reader = null;
  282. String resultJson = "";
  283.  
  284. @Override
  285. protected String doInBackground(Void... params) {
  286. try {
  287. URL url = new URL("URL");
  288.  
  289. urlConnection = (HttpURLConnection) url.openConnection();
  290. urlConnection.setRequestMethod("GET");
  291. urlConnection.connect();
  292.  
  293. InputStream inputStream = urlConnection.getInputStream();
  294. StringBuffer buffer = new StringBuffer();
  295.  
  296. reader = new BufferedReader(new InputStreamReader(inputStream));
  297.  
  298. String line;
  299. while ((line = reader.readLine()) != null) {
  300. buffer.append(line);
  301. }
  302.  
  303. resultJson = buffer.toString();
  304.  
  305. }catch (Exception e) {
  306. e.printStackTrace();
  307. }
  308. return resultJson;
  309. }
  310.  
  311. @Override
  312. protected void onCancelled() {
  313. super.onCancelled();
  314. }
  315.  
  316. @Override
  317. protected void onPostExecute(String strJson) {
  318. super.onPostExecute(strJson);
  319.  
  320. JSONObject dataJsonObj = null;
  321. String replaceStr = "(***.ru)";
  322.  
  323. try {
  324. dataJsonObj = new JSONObject(strJson);
  325. JSONArray radioArray = dataJsonObj.getJSONArray("stations");
  326.  
  327. for (int i = 0; i < radioArray.length(); i++) {
  328. JSONObject radioObject = radioArray.getJSONObject(i);
  329.  
  330. String radioUrl = radioObject.optString("url");
  331. String title = radioObject.optString("title");
  332.  
  333. Log.d(LOG_TAG, radioUrl);
  334. Log.d(LOG_TAG, title);
  335.  
  336. String radioTitle = "";
  337. try {
  338. radioTitle = new String(title.getBytes("Cp1252"),"Cp1251");
  339. radioTitle = new String(radioTitle.getBytes(),"utf-8");
  340. radioTitle = radioTitle.toLowerCase();
  341.  
  342. if (radioTitle.contains(replaceStr)) {
  343. radioTitle = radioTitle.replace(replaceStr, "");
  344. }
  345.  
  346. } catch (UnsupportedEncodingException ex) {
  347. ex.printStackTrace();
  348. }
  349.  
  350. if (isPlay) {
  351. if (radioUrl.equals(stream)) {
  352. txtTitle.setText(radioTitle.toUpperCase());
  353. }
  354. } else {
  355. txtTitle.setText("");
  356. }
  357. }
  358. } catch (JSONException e) {
  359. e.printStackTrace();
  360. }
  361. }
  362. }
  363. }
  364.  
  365. java.lang.RuntimeException:
  366. at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3045)
  367. at android.app.ActivityThread.access$2200 (ActivityThread.java:157)
  368. at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1454)
  369. at android.os.Handler.dispatchMessage (Handler.java:102)
  370. at android.os.Looper.loop (Looper.java:148)
  371. at android.app.ActivityThread.main (ActivityThread.java:5525)
  372. at java.lang.reflect.Method.invoke (Native Method)
  373. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
  374. at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)
  375. Caused by: java.lang.NullPointerException:
  376. at ru.umagadzhi.caucasusradios.PlayerService.onStartCommand (PlayerService.java:99)
  377. at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3028)
  378.  
  379. Player.btnPLayPause.setImageResource(R.drawable.icon_play);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement