在一个Android application 调用另一个 application 里的 Activity 和 Service
2011-11-15 19:16
429 查看
Android application 和普通的应用程序有些差别,他是由四大组件(compoment)组成的,即Activity,Service,ContentProvider ,和BroadcastReceiver。 本文介绍从一个application 调用其它application 里的 Activity 和 Service。
要启动别的app里的 Activity 和 Service,主要生成两个 intent 实例,分别把实例的action 设置成和另一个 application 里 AndroidManifest.xml定义的 intent-filter 里的Action一样:Intent::setAction(Action name)。
启动另一个app里的Activity:
Intent testActivityIntent = new Intent();
testActivityIntent.setAction("com.android.1234F");
startActivity(testActivityIntent);
com.android.1234F就是在另一个App里AndroidManifest.xml中对相应Activity定义的Action,请看
<activity android:name=".SecondAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.android.1234F" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
接着,启动另一个app里的Service:
testActivityIntent = null ;
testActivityIntent = new Intent();
testActivityIntent.setAction("android.intent.action.START_PCM_PLAY_SERVICE") ;
stopService(testActivityIntent);
startService(testActivityIntent);
android.intent.action.START_PCM_PLAY_SERVICE
就是另一个app里AndroidManifest.xml中对相应Service定义的Action,请看
<service android:enabled="true"
android:exported="true"
android:name=".ServiceInSecondAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.START_PCM_PLAY_SERVICE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
另一个 app里的 Activity就照正常的规则来书,这里展出了一个 Service 播放pcm的例子:
public class ServiceInSecondAndroid extends Service {
private AudioTrack at = null ;
@Override
public void onCreate()
{
super.onCreate();
Log.v("tiantian", "thirdAndroid.onCreate()");
if( at != null )
{
at.release() ;
}
at = new AudioTrack( AudioManager.STREAM_MUSIC , 44100 , AudioFormat.CHANNEL_OUT_STEREO ,AudioFormat.ENCODING_PCM_16BIT , 44100*4*25, AudioTrack.MODE_STATIC ) ;
if( at!= null )
Log.v("tiantian", "AudioTrack success create");
FileInputStream f1 ;
try
{
f1=new FileInputStream("/sdcard/gaosu_44100_stereo.pcm");
}
catch ( IOException e )
{
Log.v("tiantian", "gaosu_44100_stereo.pcm not exist");
return;
}
byte[] payload = new byte[44100*4*25] ;
Log.v("tiantian", "payload ok");
int len ;
try
{
len = f1.read(payload, 0, 44100*4*25);
}
catch (IOException e )
{
Log.v("tiantian", "fail to read gaosu_44100_stereo.pcm");
return ;
}
try
{
f1.close();
}
catch (IOException e )
{
Log.v("tiantian", "close error");
return;
}
at.write(payload,0,len);
at.play();
return ;
}
public IBinder onBind(Intent intent) {
Log.v("tiantian", "onBind");
return null;
}
}
要启动别的app里的 Activity 和 Service,主要生成两个 intent 实例,分别把实例的action 设置成和另一个 application 里 AndroidManifest.xml定义的 intent-filter 里的Action一样:Intent::setAction(Action name)。
启动另一个app里的Activity:
Intent testActivityIntent = new Intent();
testActivityIntent.setAction("com.android.1234F");
startActivity(testActivityIntent);
com.android.1234F就是在另一个App里AndroidManifest.xml中对相应Activity定义的Action,请看
<activity android:name=".SecondAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.android.1234F" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
接着,启动另一个app里的Service:
testActivityIntent = null ;
testActivityIntent = new Intent();
testActivityIntent.setAction("android.intent.action.START_PCM_PLAY_SERVICE") ;
stopService(testActivityIntent);
startService(testActivityIntent);
android.intent.action.START_PCM_PLAY_SERVICE
就是另一个app里AndroidManifest.xml中对相应Service定义的Action,请看
<service android:enabled="true"
android:exported="true"
android:name=".ServiceInSecondAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.START_PCM_PLAY_SERVICE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
另一个 app里的 Activity就照正常的规则来书,这里展出了一个 Service 播放pcm的例子:
public class ServiceInSecondAndroid extends Service {
private AudioTrack at = null ;
@Override
public void onCreate()
{
super.onCreate();
Log.v("tiantian", "thirdAndroid.onCreate()");
if( at != null )
{
at.release() ;
}
at = new AudioTrack( AudioManager.STREAM_MUSIC , 44100 , AudioFormat.CHANNEL_OUT_STEREO ,AudioFormat.ENCODING_PCM_16BIT , 44100*4*25, AudioTrack.MODE_STATIC ) ;
if( at!= null )
Log.v("tiantian", "AudioTrack success create");
FileInputStream f1 ;
try
{
f1=new FileInputStream("/sdcard/gaosu_44100_stereo.pcm");
}
catch ( IOException e )
{
Log.v("tiantian", "gaosu_44100_stereo.pcm not exist");
return;
}
byte[] payload = new byte[44100*4*25] ;
Log.v("tiantian", "payload ok");
int len ;
try
{
len = f1.read(payload, 0, 44100*4*25);
}
catch (IOException e )
{
Log.v("tiantian", "fail to read gaosu_44100_stereo.pcm");
return ;
}
try
{
f1.close();
}
catch (IOException e )
{
Log.v("tiantian", "close error");
return;
}
at.write(payload,0,len);
at.play();
return ;
}
public IBinder onBind(Intent intent) {
Log.v("tiantian", "onBind");
return null;
}
}
相关文章推荐
- 在一个Android application 调用另一个 application 里的 Activity 和 Service
- 在一个Android application 调用另一个 application 里的 Activity 和 Service
- 在一个Android application 调用另一个 application 里的 Activity 和 Service
- Android中一个Activity调用另一个Activity — Intent对象的使用
- android 一个activity调用另一个activity,窗口化显示
- android一个应用的activity调用另一个应用的activity
- Android:一个apk中调用另一个apk的activity
- Android 学习历程之二 如何在一个Service中调用Activity(Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
- android调用远程service(一个apk调用另一个apk的service)
- 在一个 application 中调用另一个 application 里的 Activity
- Android之Content和activity、service、Application关系和attachBaseContext函数调用的时候
- Android 一个Activity调用另一个Activity中的公有方法
- Android在一个应用中启动另一个应用的Activity的办法
- Android开发_如何退出已调用多个Activity的Application
- android Acitivity启动时全透明设置 一个应用调用另一个应用中的某些功能
- Android开发中在一个Activity中关闭另一个Activity
- Android Application Launch [ 创建进程--〉绑定App-->启动Activity/Start Service/...]
- Android开发笔记---如何添加一个新的activity并实现一个activity到另一个activity的转换以及关闭自身
- Android提高篇 - 如何安全退出已调用多个Activity的Application?
- Android应用是否可以只有一个Service或Broadcast Reciver,而没有Activity?