创建没有activity的apk

Android 码拜 8年前 (2016-06-02) 2052次浏览
想创建一个apk,没有activity,通过broadcast接受广播,启动应用,看网上说可以,但是实际验证时不行,启动不了,请帮看下是不是哪儿写错了?
服务类定义:
public class audioService extends Service{
private String TAG = “audioTest”;
public void onCreate(){
super.onCreate();
Log.d(TAG, “AAAAAAAAAAAAAAAAAAAAAAA”);
Toast.makeText(getApplicationContext(), “默认Toast样式”,
Toast.LENGTH_LONG).show();
}
public IBinder onBind(Intent intent){
Log.d(TAG, “BBBBBBBBBBBBBBBBBBBBBBB”);
Toast.makeText(getApplicationContext(), “不默认Toast样式”,
Toast.LENGTH_LONG).show();
return null;
}
}
广播接收器类定义:
public class audioReceiver extends BroadcastReceiver {
String TAG = “audioTest”;
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.d(TAG, “WWWWWWWWWWWWWWWWWWWWWWWW”);
Intent mBootIntent = new Intent(arg0, audioService.class);
arg0.startService(mBootIntent);
Log.d(TAG, “CCCCCCCCCCCCCCCCCCCCCCCC”);
Toast toast = Toast.makeText(arg0, “audioReceiver started”, Toast.LENGTH_SHORT);
toast.show();
String recevive = arg1.getAction();
Log.d(TAG, recevive);
}
}
AndroidManifest.xml定义:
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.audiotest”>
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<service android:name=”com.audiotest.service.audioService”></service>
<receiver android:name=”com.audiotest.service.audioReceiver”>
<intent-filter>
<action android:name=”android.intent.action.BOOT_COMPLETED” />
<action android:name=”android.audioTest.action.AUDIO_TEST_SAMPLE” />
</intent-filter>
</receiver>
</application>
<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />
</manifest>
解决方案

20

你这样坑定不行,你包名都没有,谁知道你要启动哪个程序,那要是本人写十个APP,都有个叫activity.class的类,不是十个都启动了。
可以这样,要有包名加类名:
ntent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);

或这样:
Intent intent = new Intent();
PackageManager packageManager = this.getPackageManager();
intent = packageManager.getLaunchIntentForPackage(packageName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP) ;
this.startActivity(intent);

20

权限问题吧,自启动这个权限比较特别,应用安装后必须用户手动启动一次后才起作用
假如只是安装,没有启动过的话是不行的,而你这里没有Activity,用户没办法点击图标手动启动
这样设定主要是为了防止一些恶意程序

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明创建没有activity的apk
喜欢 (0)
[1034331897@qq.com]
分享 (0)