当我尝试运行此代码时,我收到有关strictMode线程策略的错误,所以我添加了这一行。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
我后来发现这不是我在Thread或ansync任务中运行它的最佳方法,所以我想在线程或Asynctask中运行它。请在回答时帮助编辑我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_live_streaming);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
abm = new ActionBarMenu(LiveStreaming.this);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
if (InternetStatus.getInstance(this).isOnline(this)) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
// NodeList nl = doc.getElementsByTagName();
NodeList studentList = doc.getElementsByTagName("streamingurl");
// Print total student elements in document
// System.out.println("Total students: " + studentList.getLength());
Toast.makeText(getBaseContext(), "Please wait while stream loads",
Toast.LENGTH_SHORT).show();
if (studentList != null && studentList.getLength() > 0) {
for (int i = 0; i < studentList.getLength(); i++) {
Node node = studentList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
NodeList nodeList = e.getElementsByTagName("andhigh_value");
theAndroid_HighValue = nodeList.item(0).getChildNodes()
.item(0).getNodeValue();
}
vid = (VideoView) findViewById(R.id.videoview);
vid.setVideoPath(theAndroid_HighValue);
// static final String KEY_IPADHIGH = "ipadhigh_value";
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vid);
// vid.setMediaController(mediaController);
vid.requestFocus();
vid.start();
videoBuffering = new ProgressDialog(LiveStreaming.this);
videoBuffering.setMessage("Loading...Please wait");
// videoBuffering.setIcon(R.drawable.ic_launcher);
// videoBuffering.setTitle(R.string.app_name);
videoBuffering.setProgressStyle(ProgressDialog.STYLE_SPINNER);
videoBuffering.show();
vid.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what,
int extra) {
// Log.e(TAG, "Error playing video");
// Toast.makeText(getBaseContext(),"No Stream Found",Toast.LENGTH_SHORT).show();
AlertDialog NetAlert = new AlertDialog.Builder(LiveStreaming.this).create();
NetAlert.setMessage("No Stream Found!");
NetAlert.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,int which) {
// here you can add functions
// finish();
}
});
NetAlert.show();
return true;
}
});
vid.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoBuffering.cancel();
}
});
}
} else {
Toast.makeText(getBaseContext(), "No Internet Connection",
Toast.LENGTH_SHORT).show();
// Toast t =
// Toast.makeText(this,"You are not online!!!!",8000).show();
Log.v("Home",
"############################You are not online!!!!");
}
}
// liveblog_tab = (LinearLayout) findViewById(R.id.liveblog_tab);
addcom_tab = (ImageView) findViewById(R.id.addcom);
liveblog = (ImageView) findViewById(R.id.addfull);
// comments = (LinearLayout) findViewById(R.id.comments);
FLAG = LIVE_BLOG;
ourBrow = (WebView) findViewById(R.id.browser);
// adding webviewclient prevents web-view launching every-time the
// web-site is visited
ourBrow.setWebViewClient(new WebViewClient());
// ourBrow.getSettings().setBuiltInZoomControls(true);
// ourBrow.getSettings().setSupportZoom(true);
ourBrow.getSettings().setJavaScriptEnabled(true);
ourBrow.getSettings().setAllowFileAccess(true);
ourBrow.loadUrl("http://");
ourBrow.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
ourBrow.loadUrl("file:///android_asset/internet.htm");
}
});
addcom_tab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// FLAG = COMMENT;
// changeView();
viewCategory();
}
});
liveblog.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(LiveStreaming.this, FullVideoView.class);
i.putExtra("video_url", "theAndroid_HighValue");
startActivity(i);
}
});
}
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以尝试以下内容。
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new asyn().execute();
}
});
然后像这样创建你的asynctask。
class asyn extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(yourActivityname.this);
pDialog.setMessage("Loading... Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
if (InternetStatus.getInstance(this).isOnline(this)) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
// NodeList nl = doc.getElementsByTagName();
NodeList studentList = doc.getElementsByTagName("streamingurl");
// Print total student elements in document
// System.out.println("Total students: " + studentList.getLength());
Toast.makeText(getBaseContext(), "Please wait while stream loads",
Toast.LENGTH_SHORT).show();
if (studentList != null && studentList.getLength() > 0) {
for (int i = 0; i < studentList.getLength(); i++) {
Node node = studentList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
NodeList nodeList = e.getElementsByTagName("andhigh_value");
theAndroid_HighValue = nodeList.item(0).getChildNodes()
.item(0).getNodeValue();
}
vid = (VideoView) findViewById(R.id.videoview);
vid.setVideoPath(theAndroid_HighValue);
// static final String KEY_IPADHIGH = "ipadhigh_value";
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(vid);
// vid.setMediaController(mediaController);
vid.requestFocus();
vid.start();
videoBuffering = new ProgressDialog(LiveStreaming.this);
videoBuffering.setMessage("Loading...Please wait");
// videoBuffering.setIcon(R.drawable.ic_launcher);
// videoBuffering.setTitle(R.string.app_name);
videoBuffering.setProgressStyle(ProgressDialog.STYLE_SPINNER);
videoBuffering.show();
vid.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what,
int extra) {
// Log.e(TAG, "Error playing video");
// Toast.makeText(getBaseContext(),"No Stream Found",Toast.LENGTH_SHORT).show();
AlertDialog NetAlert = new AlertDialog.Builder(LiveStreaming.this).create();
NetAlert.setMessage("No Stream Found!");
NetAlert.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,int which) {
// here you can add functions
// finish();
}
});
NetAlert.show();
return true;
}
});
vid.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoBuffering.cancel();
}
});
}
} else {
Toast.makeText(getBaseContext(), "No Internet Connection",
Toast.LENGTH_SHORT).show();
// Toast t =
// Toast.makeText(this,"You are not online!!!!",8000).show();
Log.v("Home",
"############################You are not online!!!!");
}
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
此处pDialog
是您的ProgressDialog,您可以像这样定义ProgressDialog pDialog;
或者如果您不希望在按钮上显示此事件;单击事件然后您可以直接在此处使用此行onCreate方法new asyn().execute();
。
修改强>
我已经编辑了我的答案,但你必须对此做一些必要的修改,我想它会要求你在某处添加try and catch
块,然后只需添加它。
答案 2 :(得分:0)
splashHandler.sendEmptyMessageDelayed(0, 3000);
Handler splashHandler= new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case 0:
//Your code is here....
break;
default:
break;
}
}
};