BroadcastReciever未在Android中启动Service

时间:2016-05-06 17:40:25

标签: android

我在Android应用程序中创建了Service,即使应用程序未运行,我也想运行它。我为此编写了这段代码(如下所示)。此代码应以5秒的间隔连续生成通知。我已经扩展了BroadcastReceiver类,并且我已经覆盖了它的方法public void onReceive(Context context, Intent intent)以实现我的目的。但这不会生成通知(即使应用程序正在运行)。

我在AndroidManifest.xml文件中添加了这个:

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

<service android:name=".HelloService"
            android:exported="false"/>

<receiver android:name="com.example.abc.project1.MyBroadcastreceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

这是HelloService.java类:我已经定义了我的服务

package com.example.abc.project1;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class HelloService extends Service {

    private static final String TAG = "HelloService";
    private boolean isRunning  = false;

    @Override
    public void onCreate() {
        Log.i(TAG, "Service onCreate");
        isRunning = true;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "Service onStartCommand");
        //Creating new thread for my service
        //Always write your long running tasks in a separate thread, to avoid ANR
        new Thread(new Runnable() {
            @Override
            public void run() {
                //Your logic that service will perform will be placed here
                //In this example we are just looping and waits for 1000 milliseconds in each loop.
                for(int i=0; i<20; i++) {
                    try {
                        Thread.sleep(5000);
                        new NotGen().onReceive(MainActivity.ctx, new Intent());
                    } catch (Exception e) {
                    }
                    if(isRunning){
                        Log.i(TAG, "Service running");
                    }
                }
                //Stop service once it finishes its task
                stopSelf();
            }
        }).start();

        return Service.START_STICKY;
    }


    @Override
    public IBinder onBind(Intent arg0) {
        Log.i(TAG, "Service onBind");
        return null;
    }

    @Override
    public void onDestroy() {
        isRunning = false;
        Log.i(TAG, "Service onDestroy");
    }
}

这是NotGen.java类:

package com.example.abc.project1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v7.app.NotificationCompat;


public class NotGen {

    public void onReceive(Context context, Intent intent) {

        PendingIntent pIntent = PendingIntent.getActivity(context,0,intent,0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setTicker("Ticker Title");
        mBuilder.setSmallIcon(R.drawable.my_img);
        mBuilder.setContentTitle("Notification Alert, Click Me!");
        mBuilder.setContentText("Hi, This is Android Notification Detail!");
        mBuilder.setContentIntent(pIntent).getNotification();
        mBuilder.setAutoCancel(true);
        mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0,mBuilder.build());
    }

}

最后这是MyBroadcatreciever.java类:

package com.example.abc.project1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class MyBroadcastreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(MainActivity.ctx, HelloService.class);
        MainActivity.ctx.startService(startServiceIntent);
    }
}

0 个答案:

没有答案