I launch from the widget service, will start the media player, it starts to play but when it comes to AsincTask I get an error ExceptionInInitializerError
FATAL EXCEPTION: Timer-0
java.lang.ExceptionInInitializerError
at com.spynetstation.MediaService$2.run(MediaService.java:230)
at java.util.Timer$TimerImpl.run(Timer.java:284)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607)
at android.os.AsyncTask.<clinit>(AsyncTask.java:190)
... 2 more
What's the problem I can not understand?
Code Service
public class MediaService extends Service implements OnPreparedListener,OnCompletionListener{
static boolean isPlayingMain = false;
static boolean isPlayingLiquid = false;
static boolean isPlayingDubstep = false;
static MediaPlayer mediaPlayer;
static NotificationManager nm;
private static NotificationCompat.Builder mBuilder;
private static Notification.Builder mNBuilder;
public static Context ctx;
static String currentlyPlaying = null;
static String dataPlaying = null;
static String soursetrack = null;
static TimerTask repeatTask;
static Timer t;
Node node;
static String filePath;
static File file;
public static String[] his = new String [6];
static BufferedReader br;
static String meta = null;
static String metain = null;
static boolean checkstr = true;
static boolean first = false;
static boolean only_his = false;
//Widget
static boolean widget_listtrack = false;
static Context context;
static Intent intent;
static Handler h;
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.i("MediaService", "prepareAsync");
}
public static void startMP(){
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
Log.i("MediaService", "start");
doRepeatTask();
}
});
}
public static void stopMP() {
if (mediaPlayer != null) {
try {
mediaPlayer.stop();
Log.i("MediaService", "stop");
stopRepeatTask();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public static void releaseMP() {
if (mediaPlayer != null) {
try {
mediaPlayer.release();
Log.i("MediaService", "release");
mediaPlayer = null;
}
catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.i("MediaService", "onCompletion");
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.i("MediaService", "onPrepareed");
}
public void onCreate() {
super.onCreate();
ctx = getApplicationContext();
//notif(titleNotif, contentNotif);
//this.nm = ((NotificationManager)getSystemService("notification"));
}
public void onDestroy() {
//this.nm.cancelAll();
stopForeground(true);
if(CallReceiver.telManager != null) {
CallReceiver.telManager.listen(CallReceiver.phoneListener, PhoneStateListener.LISTEN_NONE);
Log.i("CallReceiver", "Destroy");
}
}
public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) {
try {
TimeUnit.SECONDS.sleep(0);
notif(getResources().getString(R.string.title_notif),getResources().getString(R.string.title_notif));
return super.onStartCommand(paramIntent, paramInt1, paramInt2);
}
catch (InterruptedException localInterruptedException) {
for (;;) {
localInterruptedException.printStackTrace();
}
}
}
public void notif(String titleNotif, String contentNotif){
//building the notification
mBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.spy)
.setContentTitle(titleNotif)
.setTicker(contentNotif)
.setOngoing(true)
//.addAction(R.drawable.media_play, "Play", notificationIntent)
;
Intent notificationIntent = new Intent(ctx, MainActivity.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(1, mBuilder.build());
}
public static void doRepeatTask(){
Log.i("TIMER", "timer start");
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();
t.cancel();
Log.i("TIMER", "timer canceled");
}
}
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) {
Log.e("MediaService","Widget"+currentlyPlaying);
//WidgetS.widgetView.setString(R.id.listtrack, "setText", currentlyPlaying);
//AppWidgetManager mgr=AppWidgetManager.getInstance(context);
//WidgetS.Update(context,intent);
super.onPostExecute(result);
}
}
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();
}
}
Code Widget
public class WidgetS extends AppWidgetProvider {
final static String ACTION_START = "widget.start";
final static String ACTION_SETTINGS = "widget.settings";
static boolean serviceRunning = false;
Intent serviceIntent;
static SharedPreferences mSettings;
public String stream_sel;
public int stream;
public boolean checker;
public static RemoteViews widgetView;
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.i("WidgetS", "onEnabled");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i("WidgetS", "onUpdate " + Arrays.toString(appWidgetIds));
mSettings = PreferenceManager.getDefaultSharedPreferences(context);
widgetView = new RemoteViews(context.getPackageName(), R.layout.widget_small);
getSettings();
for (int i : appWidgetIds) {
updateWidget(context, appWidgetManager, i);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Log.i("WidgetS", "onDeleted " + Arrays.toString(appWidgetIds));
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.i("WidgetS", "onDisabled");
}
static void updateWidget(Context ctx, AppWidgetManager appWidgetManager,int widgetID) {
Intent startIntent = new Intent(ctx, WidgetS.class);
startIntent.setAction(ACTION_START);
startIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
PendingIntent pIntent = PendingIntent.getBroadcast(ctx, widgetID, startIntent, 0);
widgetView.setOnClickPendingIntent(R.id.start, pIntent);
appWidgetManager.updateAppWidget(widgetID, widgetView);
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
serviceIntent = new Intent(context, MediaService.class);
if (intent.getAction().equalsIgnoreCase(ACTION_START)) {
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
if (mAppWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
if(serviceRunning) {
widgetView.setInt(R.id.start, "setBackgroundResource", R.drawable.media_play);
MediaService.stopMP();
MediaService.releaseMP();
context.stopService(serviceIntent);
Log.i("Service","Stop");
MediaService.widget_listtrack = true;
Toast.makeText(context, R.string.stop_service, Toast.LENGTH_SHORT).show();
} else {
context.startService(serviceIntent);
MediaService.widget_listtrack = false;
widgetView.setInt(R.id.start, "setBackgroundResource", R.drawable.media_pause);
Log.i("Service","Start");
Toast.makeText(context, R.string.start_service, Toast.LENGTH_SHORT).show();
getSettings();
Toast.makeText(context, R.string.buffering, Toast.LENGTH_SHORT).show();
if (stream==1){
MediaService.isPlayingMain = true;
if(checker){
MediaService.initMP("http://main.spynetstation.com:9000/main_pda");
}else{
MediaService.initMP("http://main.spynetstation.com:8000/main");
}
} else if (stream==0) {
MediaService.isPlayingLiquid = true;
if(checker){
MediaService.initMP("http://liquid.spynetstation.com:9000/liquid_pda");
}else{
MediaService.initMP("http://liquid.spynetstation.com:8000/liquid");
}
} else if (stream==2) {
MediaService.isPlayingDubstep = true;
if(checker){
MediaService.initMP("http://dub.spynetstation.com:9000/dub_pda");
}else{
MediaService.initMP("http://dub.spynetstation.com:8000/dub");
}
}
MediaService.startMP();
}
serviceRunning=!serviceRunning;
updateWidget(context, AppWidgetManager.getInstance(context),mAppWidgetId);
Log.i("Widget","Update");
}
}
}
public void getSettings(){
stream_sel = mSettings.getString("stream", "1");
checker = mSettings.getBoolean("checker", false);
if (stream_sel.equals("0")) {
stream = 0;}
else if (stream_sel.equals("1")) {
stream = 1;}
else if (stream_sel.equals("2")) {
stream = 2;}
else stream = 1;
}
}
0 comments:
Post a Comment