程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> broadcast-安卓廣播啟動Service失敗的問題

broadcast-安卓廣播啟動Service失敗的問題

編輯:編程解疑
安卓廣播啟動Service失敗的問題

各位大俠好,我的Service可以在主線程啟動,輸出顯示只執行了 onCreate 方法,Service 的 onStart 有一條刪除線。但是如果用 BroatcastReceiver 來啟動的話,onCreate 也沒有輸出,請問這是為什麼呢?下面是代碼:
MyBroadcastReceiver.java

 public class MyBroadcastReceiver extends BroadcastReceiver {
    private final String TAG = "MyBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, context+"");
        Intent i = new Intent(context, NofyService.class);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        //context.startService(i);
        Log.v(TAG, context.startService(intent)+"");
        Log.v(TAG, "Receiv");
    }
}

NofyService.java

 public class NofyService extends Service {
    private final String TAG = "NofyService";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.v(TAG, "onCreate");
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.v(TAG, "onstart");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cj_finger.dyfin">

    <permission android:protectionLevel="normal" android:name=".service.NofyService"></permission>

    <!-- 權限 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- 首頁 -->
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 新建日程表 -->
        <activity android:name=".activity.NewActivity">
        </activity>
        <!-- 編輯日程表 -->
        <activity android:name=".activity.EditActivity">
        </activity>

        <!-- 接收廣播 -->
        <receiver android:name=".broadcastReceiver.MyBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.MY_BROADCAST"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <!-- 服務 -->
        <service
            android:permission=".service.NofyService"
            android:enabled="true"
            android:name=".service.NofyService"
            android:process="com.cj_finger.dyfinService" >
            <intent-filter>
                <action android:name=".service.NofyService"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

    </application>

</manifest>

最佳回答:


你這顯示調用,為什麼還要addCategory?

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved