Android App的源代码


Android App的源代码:

---------------------------------------------------------------------------------------------

// myActivity.java

package com.misoo.pk01;

import android.app.Activity;

import android.content.BroadcastReceiver;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.os.Parcel;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

publicclass myActivity extends Activity implements OnClickListener {

privatefinalintWC = LinearLayout.LayoutParams.WRAP_CONTENT;

privatefinalintFP = LinearLayout.LayoutParams.FILL_PARENT;

private Button btn, btn2, btn3;

publicTextView tv;

private IBinder ib = null;

privatefinal String MY_S_EVENT =

newString("com.misoo.pk01.myService.MY_S_EVENT");

protectedfinal IntentFilter filter=newIntentFilter(MY_S_EVENT);

private BroadcastReceiver receiver=new myIntentReceiver();

publicvoidonCreate(Bundle icicle) {

super.onCreate(icicle);

LinearLayout layout = newLinearLayout(this);

layout.setOrientation(LinearLayout.VERTICAL);

btn = newButton(this);

btn.setId(101);

btn.setText("play");

btn.setBackgroundResource(R.drawable.heart);

btn.setOnClickListener(this);

LinearLayout.LayoutParams param =

newLinearLayout.LayoutParams(80, 50);

param.topMargin = 10;

layout.addView(btn,param);

btn2 = newButton(this);

btn2.setId(102);

btn2.setText("stop");

btn2.setBackgroundResource(R.drawable.heart);

btn2.setOnClickListener(this);

layout.addView(btn2,param);

btn3 = newButton(this);

btn3.setId(103);

btn3.setText("exit");

btn3.setBackgroundResource(R.drawable.cloud);

btn3.setOnClickListener(this);

layout.addView(btn3,param);

tv = newTextView(this);

tv.setText("Ready");

LinearLayout.LayoutParams param2 = new

LinearLayout.LayoutParams(FP, WC);

param2.topMargin = 10;

layout.addView(tv,param2);

setContentView(layout);

//---------------------------------

registerReceiver(receiver, filter);

//------------------------------------------------------

bindService(newIntent("com.misoo.pk01.REMOTE_SERVICE"),

mConnection, Context.BIND_AUTO_CREATE);

}

private ServiceConnection mConnection = new ServiceConnection() {

@Override

publicvoidonServiceConnected(ComponentName className, IBinder

ibinder) {

ib = ibinder;

}

@Override

publicvoidonServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

}

};

publicvoid onClick(View v) {

switch(v.getId()) {

case101:

Parcel data = Parcel.obtain();

Parcel reply = Parcel.obtain();

try{

ib.transact(1,data, reply, 0);

} catch (Exception e) {

e.printStackTrace();

}

break;

case102:

data = Parcel.obtain();

reply = Parcel.obtain();

try{

ib.transact(2,data, reply, 0);

} catch (Exception e) {

e.printStackTrace();

}

break;

case103:

finish();

break;

}

}

//----------------------------------------------------

class myIntentReceiver extends BroadcastReceiver {

@Override

publicvoidonReceive(Context context, Intent intent) {

intbn = intent.getIntExtra("key",-1);

if(bn== 0)

tv.setText("Playing");

else

tv.setText("Stop.");

}

}

}

 

// myService.java

package com.misoo.pk01;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.*;

import android.util.Log;

publicclass myService extends Service implements Runnable {

private IBinder mBinder = null;

private Thread th1;

publicstaticHandler h;

private MediaPlayer mPlayer = null;

publicstaticContext ctx;

privatefinal String MY_S_EVENT =

newString("com.misoo.pk01.myService.MY_S_EVENT");

@Override

publicvoidonCreate() {

super.onCreate();

ctx= this;

mBinder= new myBinder();

//---------------------------------

// 诞生一个子线程及其MQ

// 等待Message

//---------------------------------

th1 = newThread(this);

th1.start();

}

@Override

publicIBinder onBind(Intent intent) {

returnmBinder;

}

publicvoidrun() {

Looper.prepare();

h = newEventHandler(Looper.myLooper());

Looper.loop();

}

//---------------------------------------

classEventHandler extends Handler {

public EventHandler(Looper looper) {

super(looper);

}

publicvoid handleMessage(Message msg) {

String obj = (String)msg.obj;

if(obj.contains("play"))

{

if(mPlayer!= null) return;

//----------------------------------

Intent in = new Intent(MY_S_EVENT);

in.putExtra("key", 0);

ctx.sendBroadcast(in);

//----------------------------------

mPlayer= MediaPlayer.create(ctx, R.raw.dreamed);

try{

mPlayer.start();

} catch(Exception e) {

Log.e("Play", "error: " + e.getMessage(), e);

}

}

elseif(obj.contains("stop")) {

if(mPlayer != null) {

//----------------------------------

Intent in = newIntent(MY_S_EVENT);

in.putExtra("key", 1);

ctx.sendBroadcast(in);

//----------------------------------

mPlayer.stop();

mPlayer.release();

mPlayer= null;

}

}

}}}

// myBinder.java

package com.misoo.pk01;

import android.os.Binder;

import android.os.Message;

import android.os.Parcel;

publicclass myBinder extends Binder{

@Override

publicboolean onTransact(int code, Parcel data, Parcel reply, int flags)

throws android.os.RemoteException {

switch(code){

case 1:

// 将Message丢到子线程的MQ to draw Graphic

String obj = "play";

Message msg = myService.h.obtainMessage(1,1,1,obj);

myService.h.sendMessage(msg);

break;

case 2:

// 将Message丢到子线程的MQ to stop drawing

obj = "stop";

msg = myService.h.obtainMessage(1,1,1,obj);

myService.h.sendMessage(msg);

break;

}

returntrue;

}}








// myActivity.java

package com.misoo.pk01;

import android.app.Activity;

import android.content.BroadcastReceiver;

import android.content.ComponentName;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.os.Parcel;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

publicclass myActivity extends Activity implements OnClickListener {

privatefinalintWC = LinearLayout.LayoutParams.WRAP_CONTENT;

privatefinalintFP = LinearLayout.LayoutParams.FILL_PARENT;

private Button btn, btn2, btn3;

publicTextView tv;

private IBinder ib = null;

privatefinal String MY_S_EVENT =

newString("com.misoo.pk01.myService.MY_S_EVENT");

protectedfinal IntentFilter filter=newIntentFilter(MY_S_EVENT);

private BroadcastReceiver receiver=new myIntentReceiver();

publicvoidonCreate(Bundle icicle) {

super.onCreate(icicle);

LinearLayout layout = newLinearLayout(this);

layout.setOrientation(LinearLayout.VERTICAL);

btn = newButton(this);

btn.setId(101);

btn.setText("play");

btn.setBackgroundResource(R.drawable.heart);

btn.setOnClickListener(this);

LinearLayout.LayoutParams param =

newLinearLayout.LayoutParams(80, 50);

param.topMargin = 10;

layout.addView(btn,param);

btn2 = newButton(this);

btn2.setId(102);

btn2.setText("stop");

btn2.setBackgroundResource(R.drawable.heart);

btn2.setOnClickListener(this);

layout.addView(btn2,param);

btn3 = newButton(this);

btn3.setId(103);

btn3.setText("exit");

btn3.setBackgroundResource(R.drawable.cloud);

btn3.setOnClickListener(this);

layout.addView(btn3,param);

tv = newTextView(this);

tv.setText("Ready");

LinearLayout.LayoutParams param2 = new

LinearLayout.LayoutParams(FP, WC);

param2.topMargin = 10;

layout.addView(tv,param2);

setContentView(layout);

//---------------------------------

registerReceiver(receiver,filter);

//------------------------------------------------------

bindService(newIntent("com.misoo.pk01.REMOTE_SERVICE"),

mConnection, Context.BIND_AUTO_CREATE);

}

private ServiceConnection mConnection = new ServiceConnection() {

@Override

publicvoidonServiceConnected(ComponentName className, IBinder

ibinder) {

ib = ibinder;

}

@Override

publicvoidonServiceDisconnected(ComponentName name) {

// TODO Auto-generated method stub

}

};

publicvoid onClick(View v) {

switch(v.getId()) {

case101:

Parcel data = Parcel.obtain();

Parcel reply = Parcel.obtain();

try{

ib.transact(1,data, reply, 0);

} catch (Exception e) {

e.printStackTrace();

}

break;

case102:

data = Parcel.obtain();

reply = Parcel.obtain();

try{

ib.transact(2,data, reply, 0);

} catch (Exception e) {

e.printStackTrace();

}

break;

case103:

finish();

break;

}

}

//----------------------------------------------------

class myIntentReceiver extends BroadcastReceiver {

@Override

publicvoidonReceive(Context context, Intent intent) {

intbn = intent.getIntExtra("key",-1);

if(bn== 0)

tv.setText("Playing");

else

tv.setText("Stop.");

}

}

}

 

// myService.java

package com.misoo.pk01;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.*;

import android.util.Log;

publicclass myService extends Service implements Runnable {

private IBinder mBinder = null;

private Thread th1;

publicstaticHandler h;

private MediaPlayer mPlayer = null;

publicstaticContext ctx;

privatefinal String MY_S_EVENT =

newString("com.misoo.pk01.myService.MY_S_EVENT");

@Override

publicvoidonCreate() {

super.onCreate();

ctx= this;

mBinder= new myBinder();

//---------------------------------

// 诞生一个子线程及其MQ

// 等待Message

//---------------------------------

th1 = newThread(this);

th1.start();

}

@Override

publicIBinder onBind(Intent intent) {

returnmBinder;

}

publicvoidrun() {

Looper.prepare();

h = newEventHandler(Looper.myLooper());

Looper.loop();

}

//---------------------------------------

classEventHandler extends Handler {

public EventHandler(Looper looper) {

super(looper);

}

publicvoid handleMessage(Message msg) {

String obj = (String)msg.obj;

if(obj.contains("play"))

{

if(mPlayer!= null) return;

//----------------------------------

Intent in = newIntent(MY_S_EVENT);

in.putExtra("key", 0);

ctx.sendBroadcast(in);

//----------------------------------

mPlayer= MediaPlayer.create(ctx, R.raw.dreamed);

try{

mPlayer.start();

} catch(Exception e) {

Log.e("Play", "error: " + e.getMessage(), e);

}

}

elseif(obj.contains("stop")) {

if(mPlayer != null) {

//----------------------------------

Intent in = newIntent(MY_S_EVENT);

in.putExtra("key", 1);

ctx.sendBroadcast(in);

//----------------------------------

mPlayer.stop();

mPlayer.release();

mPlayer= null;

}

}

}}}

// myBinder.java

package com.misoo.pk01;

import android.os.Binder;

import android.os.Message;

import android.os.Parcel;

publicclass myBinder extends Binder{

@Override

publicboolean onTransact(int code, Parcel data, Parcel reply, int flags)

throws android.os.RemoteException {

switch(code){

case 1:

// 将Message丢到子线程的MQ to draw Graphic

String obj = "play";

Message msg = myService.h.obtainMessage(1,1,1,obj);

myService.h.sendMessage(msg);

break;

case 2:

// 将Message丢到子线程的MQ to stop drawing

obj = "stop";

msg = myService.h.obtainMessage(1,1,1,obj);

myService.h.sendMessage(msg);

break;

}

returntrue;

}}

~ End ~

时间: 2024-11-08 19:18:30

Android App的源代码的相关文章

Android App组件之ListFragment -- 说明和示例(转载)

转自:http://www.cnblogs.com/skywang12345/p/3160260.html 1 ListFragement介绍 ListFragment继承于Fragment.因此它具有Fragment的特性,能够作为activity中的一部分,目的也是为了使页面设计更加灵活. 相比Fragment,ListFragment的内容是以列表(list)的形式显示的. 1.1 ListFragment布局 ListFragment的布局默认包含一个list view.因此,在List

android app自动化测试之UIAutomator

一.UIAutomator Android自动化测试工具有很多,但是要免费.易上手,本人觉得就直接使用Eclipse自带的UIAutomator就不错.测试人员无需跟开发要代码信息,只要手机上有安装之后的APP自己就能做出自动测试用例,况且一通百通,就算是不满足于UI测试的,找个简单易上手的先明白原理,再深入了解其它复杂工具也会轻松很多.何乐而不为呢? UIAutomator是Eclipse自带的用于UI自动化测试工具,可仿真APP上的单击.滑动.输入文本等操作. 在使用之前,需要安装好java

Android Wifi子系统源代码View

本文基于Android 4.2.2+Linux3.6.9+SAMA5D3 SoC从源代码的角度审视Android Wifi子系统. 软件平台:Linux3.6.9 + Android 4.2.2 硬件平台:Atmel SAMA5 Wifi模组:RTL8723AU(USB接口) Android的WiFi子系统自上而下包括如下一些内容: 应用层 Androd系统自带Settings应用 /system/app/Settings.apk http://androidxref.com/4.2.2_r1/

老李分享:android app自动化测试工具合集

老李分享:android app自动化测试工具合集 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-84505200.我们从2016年8月开始不断升级测试开发工程师就业培训的班的课程,不断新增和优化课程内容,为了和当下企业的实际情况,提高学员的实战水平,在2016年稳定课程主题框架,做到每半年升级一版. 安卓应用自动化测试工具之一 - PerfectoMob

Android 4.0 源代码结构

21.Android 4.0 源代码结构 Android源码的第一级目录结构 Android/abi (abi相关代码.ABI:application binary interface,应用程序二进制接口) Android/bionic(bionic C库) Android/bootable(启动引导相关代码) Android/build(存放系统编译规则及generic等基础开发配置包) Android/cts(Android兼容性测试套件标准) Android/dalvik(dalvik J

Android APP 调试过程中遇到的问题。

调试过过程中APP安装完启动后有的时候会异常退出,报这个错误.有的时候可以直接启动.查找不到原因.网上说把commit方法替换成commitAllowingStateLoss() 也无效. Android APP 调试过程中遇到的问题. >> android 这个答案描述的挺清楚的:http://www.goodpm.net/postreply/android/1010000007192169/AndroidAPP调试过程中遇到的问题.html

MAC安裝《Genymotion Android模擬器》大玩Android APP (神魔之塔)

链接地址:http://www.minwt.com/mac/10083.html/comment-page-2 MAC» 智慧型裝罝» Android | 2014/02/12 Android是一個開放的平台,因此先前也分享了幾個Android的模擬器,但當初梅干使用Android模擬器,最主要的功能就是用來測試網頁,看網頁在Android手機上是否能正常運作,雖然說這些Android模擬器,也可透過Google Player安裝Android APP,在電腦中就可玩Android APP,但由

使用javah生成.h文件, 出现无法访问android.app,Activity的错误的解决

在工程ndk22/bin/classes中 运行javah  com.cn.ndk22.Ndk22.Activity ,出现了.h文件 我在bin/classes目录中 ,就是无法访问, : 错误:无法访问android.app.Activity 找不到android.app.Activity 如下图所示 于是我cmd定位到ndk/src,中运行 javah com.heima.ndk.ndkActivity, 成功了就能成功了 ...我也不知道为什么.,如下图 总结:  使用javah生成.h

学习Android app 的TAB页面切换

FragmentActivity+ ViewPager + FragmentManager实现 废话少说,直接上我今晚的成果: package com.itfanr.viewpagertab; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.support.v4.app.FragmentTransaction  ; import android.suppo