AMcoder - javascript, python, java, html, php, sql

Ծառայության դադարեցում Android-ում

Ես ունեմ Android Ծառայություն, որն աշխատում է միշտ ֆոնային ռեժիմում, և մեկ այլ ծառայություն, որը գործարկվում է միշտ գործող ծառայության կողմից, որը ահազանգման կառավարիչ ծառայությունն է:

Այնուամենայնիվ, ես ուզում եմ դադարեցնել ծառայությունը կոճակով, իմ նպատակն է դադարեցնել միշտ գործարկվող ծառայությունը, որպեսզի ազդանշանային կառավարչի ծառայությունը ավտոմատ կերպով դադարեցվի: Արդյո՞ք դա ճիշտ տեսակետ է:

Իմ օրինակելի կոդը հետևյալն է

package com.example.deneme;



public class AndroidNotifyService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button buttonStartService = (Button)findViewById(R.id.startservice);
    Button buttonStopService = (Button)findViewById(R.id.stopservice);

    buttonStartService.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(AndroidNotifyService.this, com.example.deneme.AndroidScheduledService.class);
            AndroidNotifyService.this.startService(intent);
        }});

    buttonStopService.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent intent = new Intent();
            intent.setAction(AndroidScheduledService.ACTION);
            intent.putExtra("RQS", AndroidScheduledService.RQS_STOP_SERVICE);
            sendBroadcast(intent);


        }});

}

}

Իմ միշտ աշխատող ծառայությունը

package com.example.deneme;


public class AndroidScheduledService extends Service {


final static String ACTION = "AndroidScheduledServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;

public int onStartCommand(Intent intent, int flags, int startId) {
   // TODO Auto-generated method stub
    Intent myIntent = new Intent(getBaseContext(),
      MyScheduledReceiver.class);

    PendingIntent pendingIntent
     = PendingIntent.getBroadcast(getBaseContext(),
       0, myIntent, 0);

    AlarmManager alarmManager
      = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    long interval = 60 * 1000; //
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
      calendar.getTimeInMillis(), interval, pendingIntent);




    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    //this.unregisterReceiver(notifyServiceReceiver);
    Intent intent = new Intent();
    intent.setAction(NotifyService.ACTION);
    intent.putExtra("RQS", NotifyService.RQS_STOP_SERVICE);
    sendBroadcast(intent);
    super.onDestroy();
}

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


}

Իմ ազդանշանային մենեջերի ծառայությունը

public class NotifyService extends Service {

final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;

HttpClient httpclnt;
HttpPost httppst;
String message;
String response;



//NotifyServiceReceiver notifyServiceReceiver;

private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";

/*
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    notifyServiceReceiver = new NotifyServiceReceiver();
    super.onCreate();
}
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    /*
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ACTION);
    registerReceiver(notifyServiceReceiver, intentFilter);

*/  

    // Send Notification


    notificationManager = 
        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(R.drawable.ic_launcher, 
            "Notification!", 
            System.currentTimeMillis());
    Context context = getApplicationContext();
    String notificationTitle = "Exercise of Notification!";
    String notificationText = "http://www.google.com/";
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
    PendingIntent pendingIntent 
            = PendingIntent.getActivity(getBaseContext(), 
                    0, myIntent, 
                    Intent.FLAG_ACTIVITY_NEW_TASK);
    myNotification.defaults |= Notification.DEFAULT_SOUND;
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    myNotification.setLatestEventInfo(context, 
                notificationTitle, 
                notificationText, 
                pendingIntent);
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);




    return super.onStartCommand(intent, flags, startId);
}

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


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    //this.unregisterReceiver(notifyServiceReceiver);
    super.onDestroy();
}
/*
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public class NotifyServiceReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        int rqs = arg1.getIntExtra("RQS", 0);
        if (rqs == RQS_STOP_SERVICE){
            stopSelf();
        }
    }
}*/

}

Իմ հեռարձակման ընդունիչի դասերը

NotifyService Class փաթեթի համար com.example.deneme;

public class MyScheduledReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

Intent intent2 = new Intent(context, com.example.deneme.NotifyService.class);
context.startService(intent2);

  }

}

AndroidScheduledService դասի համար

package com.example.deneme;


public class AutoStartNotifyReceiver extends BroadcastReceiver {

private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
        Intent myIntent = new Intent(context, com.example.deneme.AndroidScheduledService.class);
        context.startService(myIntent);
    }


}

}

Պատասխանները:


1

Ծառայության դադարեցումը չի դադարեցնի Զարթուցիչի կառավարչի ծառայությունը: Դուք պետք է դադարեցնեք այն ձեռքով:

Intent intent = new Intent(this, com.example.deneme.AndroidScheduledService.class);
PendingIntent sender = PendingIntent.getBroadcast(this,
           0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.cancel(sender);

Ծառայությունը դադարեցնելու համար.

  Intent stopServiceIntent = new Intent(getBaseContext(), yourServiceToStop.class);
  getBaseContext().stopService(stopServiceIntent );
02.11.2012
  • Ի՞նչը չի աշխատում: Չե՞ք կարող չեղարկել կրկնվող ահազանգը կամ չե՞ք կարող դադարեցնել ծառայությունը: 02.11.2012
  • Այո, ես տեղադրել եմ այդ կոդը կոճակի սեղմման լսարանի մեջ, քանի որ ուզում եմ, որ այն դադարեցվի, երբ կոճակը սեղմվի, այն դեռ շարունակում է ծանուցում տալ: 04.11.2012
  • Զարթուցիչը չեղարկելուց հետո փորձեք դադարեցնել ծառայությունը: Տես թարմացված պատասխանը։ 05.11.2012
  • Ես փորձեցի թարմացված պատասխանը, բայց արդյունքը նույնն է: Ես ի վիճակի չեմ կանգնեցնել ծառայության և ահազանգման մենեջերին: Ես ոչ մի այլ բան չունեմ, քան վերը նշված իմ օրինակելի կոդը: 05.11.2012
  • Տարօրինակ. Դուք գրել եք երկուսն էլ stop codes կոճակի սեղմման ունկնդիրի ներսում: 05.11.2012
  • Այո, ես գրել եմ իմ դադարեցման ծածկագրերը կոճակի սեղմման ունկնդիրի ներսում 05.11.2012
  • Նոր նյութեր

    Օգտագործելով Fetch Vs Axios.Js-ը՝ HTTP հարցումներ կատարելու համար
    JavaScript-ը կարող է ցանցային հարցումներ ուղարկել սերվեր և բեռնել նոր տեղեկատվություն, երբ դա անհրաժեշտ լինի: Օրինակ, մենք կարող ենք օգտագործել ցանցային հարցումը պատվեր ներկայացնելու,..

    Տիրապետել հանգստության արվեստին. մշակողի ուղեցույց՝ ճնշման տակ ծաղկելու համար
    Տիրապետել հանգստության արվեստին. մշակողի ուղեցույց՝ ճնշման տակ ծաղկելու համար Ինչպե՞ս հանգստացնել ձեր միտքը և աշխատեցնել ձեր պրոցեսորը: Ինչպես մնալ հանգիստ և զարգանալ ճնշման տակ...

    Մեքենայի ուսուցում բանկային և ֆինանսների ոլորտում
    Բարդ, խելացի անվտանգության համակարգերը և հաճախորդների սպասարկման պարզեցված ծառայությունները բիզնեսի հաջողության բանալին են: Ֆինանսական հաստատությունները, մասնավորապես, պետք է առաջ մնան կորի..

    Ես AI-ին հարցրի կյանքի իմաստը, այն ինչ ասում էր, ցնցող էր:
    Այն պահից ի վեր, երբ ես իմացա Արհեստական ​​ինտելեկտի մասին, ես հիացած էի այն բանով, թե ինչպես է այն կարողանում հասկանալ մարդկային նորմալ տեքստը, և այն կարող է առաջացնել իր սեփական արձագանքը դրա..

    Ինչպես սովորել կոդավորումը Python-ում վագրի պես:
    Սովորելու համար ծրագրավորման նոր լեզու ընտրելը բարդ է: Անկախ նրանից, թե դուք սկսնակ եք, թե առաջադեմ, դա օգնում է իմանալ, թե ինչ թեմաներ պետք է սովորել: Ծրագրավորման լեզվի հիմունքները, դրա..

    C++-ի օրական բիթ(ե) | Ամենաերկար պալինդրոմային ենթաշարը
    C++ #198-ի ամենօրյա բիթ(ե), Ընդհանուր հարցազրույցի խնդիր. Ամենաերկար պալինդրոմային ենթատող: Այսօր մենք կանդրադառնանք հարցազրույցի ընդհանուր խնդրին. Ամենաերկար palindromic substring...

    Kydavra ICAReducer՝ ձեր տվյալների ծավալայինությունը նվազեցնելու համար
    Ի՞նչ է ICAReducer-ը: ICAReducer-ն աշխատում է հետևյալ կերպ. այն նվազեցնում է նրանց միջև բարձր փոխկապակցված հատկանիշները մինչև մեկ սյունակ: Բավականին նման է PCAreducer-ին, չնայած այն..