1
可以使用'顯式'意圖從另一個應用程序啓動服務? 它一直顯示錯誤:not allowed to start service intent without permission
如何啓動Receiver應用程序的Service
?可以使用顯式意圖從另一個應用程序啓動服務?
發送應用程序的Activity
:
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn_call);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ComponentName name = new ComponentName("com.example.vic_app_1", "com.example.vic_app_1.SendSmsService");
Intent abc = new Intent();
abc.setComponent(name);
abc.setAction(Intent.ACTION_MAIN);
abc.addCategory(Intent.CATEGORY_LAUNCHER);
abc.putExtra("destinationAddress", "5554");
ComponentName c = getApplication().startService(abc);
if (c == null) { Log.e("error", "failed to start with "+abc); }
}
接收機應用的Service
:
package com.example.vic_app_1;
public class SendSmsService extends Service {
{ @Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
Log.d("slog", "onStart()");
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
Log.d("slog", "onDestroy()");
super.onDestroy();
}
} }
接收機應用的Manifest
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vic_app_1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".SendSmsService" >
</service>
</application>
</manifest>