Android-Activity控制Service的问题
public class PoemMainActivity extends Activity implements OnClickListener {
// 翻书效果
private PageWidget mPageWidget;
Bitmap mCurPageBitmap, mNextPageBitmap;
Canvas mCurPageCanvas, mNextPageCanvas;
BookPageFactory pagefactory;
private PopupWindow mOptionsMenu;// Menu菜单项
private MyBinder mBinder;
private Handler mHandler;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initView();
initOptionsMenu();
initBook();
initHandler();
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, MediaPlayService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = (MyBinder) service;
}
};
@Override
protected void onStop() {
super.onStop();
unbindService(conn);
}
private void initView() {
// TODO Auto-generated method stub
}
private void initHandler() {
// TODO Auto-generated method stub
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
default:
break;
}
}
};
}
private void initBook() {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out
.println("屏幕分辨率为:" + dm.widthPixels + " * " + dm.heightPixels);
mPageWidget = new PageWidget(this, dm.widthPixels, dm.heightPixels);
setContentView(mPageWidget);
mCurPageBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels,
Bitmap.Config.ARGB_8888);
mNextPageBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels,
Bitmap.Config.ARGB_8888);
mCurPageCanvas = new Canvas(mCurPageBitmap);
mNextPageCanvas = new Canvas(mNextPageBitmap);
pagefactory = new BookPageFactory(dm.widthPixels, dm.heightPixels);
pagefactory.setBgBitmap(BitmapFactory.decodeResource(
this.getResources(), R.drawable.back));
intent = getIntent();
Bundle bundleExtra = intent.getBundleExtra("Bundle");
String parent = bundleExtra.getString("parent");
String childern = bundleExtra.getString("childern");
try {
if ("0".equals(parent) && "0".equals(childern)) {
pagefactory.openbook("/sdcard/1.txt");
} else {
pagefactory.openbook("/sdcard/2.txt");
}
pagefactory.onDraw(mCurPageCanvas);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Toast.makeText(this, "电子书不存在", Toast.LENGTH_SHORT).show();
}
mPageWidget.setBitmaps(mCurPageBitmap, mCurPageBitmap);
mPageWidget.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent e) {
// TODO Auto-generated method stub
boolean ret = false;
if (v == mPageWidget) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
mPageWidget.abortAnimation();
mPageWidget.calcCornerXY(e.getX(), e.getY());
pagefactory.onDraw(mCurPageCanvas);
if (mPageWidget.DragToRight()) {
try {
pagefactory.prePage();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (pagefactory.isfirstPage())
return false;
pagefactory.onDraw(mNextPageCanvas);
} else {
try {
pagefactory.nextPage();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (pagefactory.islastPage())
return false;
pagefactory.onDraw(mNextPageCanvas);
}
mPageWidget.setBitmaps(mCurPageBitmap, mNextPageBitmap);
}
ret = mPageWidget.doTouchEvent(e);
return ret;
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (mOptionsMenu.isShowing()) {
mOptionsMenu.dismiss();
} else {
mOptionsMenu.showAtLocation(mPageWidget, Gravity.BOTTOM, 0, 0);
}
return false;
}
private void initOptionsMenu() {
View contentView = LayoutInflater.from(this).inflate(
R.layout.optionsmenu, null);
// 为popupwindow增加监听时间
ImageView imgViewBack = (ImageView) contentView
.findViewById(R.id.menu_back);
imgViewBack.setOnClickListener(this);
ImageView imgViewHome = (ImageView) contentView
.findViewById(R.id.menu_home);
imgViewHome.setOnClickListener(this);
View viewClick = (View) contentView.findViewById(R.id.menu_view);
viewClick.setOnClickListener(this);
ImageView imgViewWitness = (ImageView) contentView
.findViewById(R.id.menu_witness);
imgViewWitness.setOnClickListener(this);
ImageView imgViewDownLoad = (ImageView) contentView
.findViewById(R.id.menu_download);
imgViewDownLoad.setOnClickListener(this);
ImageView imgViewSound = (ImageView) contentView
.findViewById(R.id.menu_sound);
imgViewSound.setOnClickListener(this);
ImageView imgViewStop = (ImageView) contentView
.findViewById(R.id.menu_stop);
imgViewStop.setOnClickListener(this);
ImageView imgViewProgress = (ImageView) contentView
.findViewById(R.id.menu_progress);
imgViewProgress.setOnClickListener(this);
ImageView imgViewBook = (ImageView) contentView
.findViewById(R.id.menu_book);
imgViewBook.setOnClickListener(this);
ImageView imgViewMore = (ImageView) contentView
.findViewById(R.id.menu_more);
imgViewMore.setOnClickListener(this);
mOptionsMenu = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
mOptionsMenu.setAnimationStyle(android.R.style.Animation_Dialog);
mOptionsMenu.update();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mOptionsMenu.isShowing()) {
mOptionsMenu.dismiss();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onPause() {
super.onPause();
mOptionsMenu.dismiss();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.menu_back:
Toast.makeText(this, "BACK", 1).show();
break;
case R.id.menu_home:
Toast.makeText(this, "HOME", 1).show();
break;
case R.id.menu_witness:
Toast.makeText(this, "WITNESS", 1).show();
break;
case R.id.menu_download:
downlaodSong();
break;
case R.id.menu_sound:
togglePlay();
break;
case R.id.menu_stop:
Toast.makeText(this, "STOP", 1).show();
break;
case R.id.menu_progress:
Toast.makeText(this, "PROGRESS", 1).show();
break;
case R.id.menu_book:
Toast.makeText(this, "BOOK", 1).show();
break;
case R.id.menu_more:
Toast.makeText(this, "MORE", 1).show();
break;
default:
break;
}
mOptionsMenu.dismiss();
}
private void downlaodSong() {
Toast.makeText(PoemMainActivity.this, "开始下载", 1).show();
new UpgradeAsyncTask()
.execute("http://192.168.1.131/wirelessOrder/api/music/23");
}
private class UpgradeAsyncTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String url;
try {
url = params[0];
FileOutputStream fos = null;
InputStream is = null;
URL downloadUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) downloadUrl
.openConnection();
conn.setReadTimeout(5000);
conn.connect();
int fileLength = conn.getContentLength();
if (HttpURLConnection.HTTP_OK != conn.getResponseCode()) {
return null;
}
is = conn.getInputStream();
byte[] buffer = new byte[1024];
File file = new File(Constant.DIR_PATH, "23.mp3");
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
boolean mkdirs = parentFile.mkdirs();
boolean newFile = file.createNewFile();
Log.d("download", "mkdirs : " + mkdirs + " newFile : "
+ newFile);
}
fos = new FileOutputStream(file);
int loadedLength = 0;
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(PoemMainActivity.this, "下载结束", 1).show();
super.onPostExecute(result);
}
}
private void togglePlay() {
mBinder.continuePlay();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
( ⊙ o ⊙ )啊!!! 这位大哥贴这个这串代码,看得头都疼了。。 看到了后面发现是服务绑定的问题 呵呵。代码写的不错,学习了。 剑波兄的答案,不怎么认可,bindService 这个,如果服务没有启动,会自动startservice, 我举得不存在 没有调用 startservice。服务能否绑定成功,跟androidManifest.xml 中的注册还是有关系的
<service android:name="com.mumayi.market.service.XXService"></service>
这样注册还可以写上很多的属性,如果你不是很明白建议这么写就足够了, 有个属性能让服务不被绑定,不过我忘了是啥了。。。你可以弄清楚在写。
另外还有一点,服务的绑定,是一个异步的操作,并非前一句代码绑定,后一句代码就能里面的绑定成功。 建议以这两点为出发点去思考下吧, 你代码太多。。这样看是在太累了。。。o(>﹏<)o
没有调用startservice启动服务,
@Override
public IBinder onBind(Intent intent) {
mBinder = new MyBinder();//删掉这句
return mBinder;
}
因为oncreate里面已经创建了。