-1

放送受信機の起動幅でサービスを開始します。

public class Autostart extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        Intent intent = new Intent(context, MyService.class);
        MyService.setContext(context);
        context.startService(intent);
    }
}

これは、電話を起動すると、サービスがバックグラウンドで実行されている場合に機能します。次に、GUIからサービスを停止/開始/再起動します。

Intent intent = new Intent(MyService.getContext(), MyService.class);
getActivity().stopService(intent);

しかし、それはタスクを殺すことはありません。コンテキストが同じ場合、これは機能する必要があると思ったので、静的コンテンツホルダーを作成しましたが、エーテルでは機能しません。

そのコードからサービスを開始し、開始したサービスを簡単に停止できることに注意してください。

その自動開始/バックグラウンドサービスを停止するにはどうすればよいですか?

4

1 に答える 1

1

Androidバックグラウンドサービスを呼び出すには、次の手順を実行します

まず、フェッチサービスクラスの作成を追加してから呼び出します

public class FetchUnreadMailService extends Service {

    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;
    private Timer myTimer;
    Session mySession;      
    int unread_count = 0;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        myTimer = new Timer();
        myTimer.schedule(new TimerTask() {
            @Override
            public void run() {

                    mySession = new Session();
                     if(mySession.getStatus()){
                            APIHelper api = new APIHelper();
                            unread_count =  (Integer) api.getMailBoxNotification(mySession.getUserId());
                            }
                        if(unread_count>0){
                            mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                            Intent notifyIntent = new Intent(FetchUnreadMailService.this,MailBox.class);
                            Context context = getApplicationContext();
                            CharSequence contentTitle = "New Mail";
                            CharSequence contentText = "You Have ("+ unread_count +") unread mails";
                            PendingIntent intent = PendingIntent.getActivity(FetchUnreadMailService.this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
                            final Notification notifyDetails = new Notification(R.drawable.youtube,"New Alert, Click Me!",System.currentTimeMillis());  
                            notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
                            notifyDetails.flags= Notification.FLAG_AUTO_CANCEL;//for auto cancel notification
                            mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);  
                            }   




            }

        }, 0, 30000);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
    }






}

このクラスを作成した後、次のように呼び出します

startService(new Intent(Home.this,FetchUnreadMailService.class));

詳細については、次のリンクを参照してくださいhttp://grabcodes.blogspot.com

于 2012-09-14T17:02:50.043 に答える