मेरे आवेदन में, मैं अलार्म प्रबंधक का उपयोग कर एक सेवा शुरू करने की कोशिश कर रहा हूं। जब मैं एक बटन सेवा पर क्लिक कर रहा हूं, तो मुझे एक विशेष समय में शुरू करना चाहिए जो मैं दे रहा हूं। अलार्म प्रबंधक के लिए मेरे कोड नीचे दिया गया हैAndroid में अलार्म प्रबंधक का उपयोग कर सेवा कैसे प्रारंभ करें?
public void onClick(View view)
{
if(view == m_btnActivate)
{
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
cur_cal.add(Calendar.SECOND, 50);
Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
Log.d("Testing", "Intent created");
PendingIntent pi = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi);
Log.d("Testing", "alarm manager set");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
}
और bellow मेरी सेवा क्लास है:
public class ServiceClass extends Service{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Testing", "Service got created");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show();
Log.d("Testing", "Service got started");
}
}
सेवा शुरू कर देंगे तो यह सेवा वर्ग में लॉग संदेशों को मुद्रित करना चाहिए। लेकिन यह नहीं दिख रहा है। क्या कोई मदद कर सकता है?
मैंने नाम का उल्लेख किया, लेकिन एंड्रॉइड नहीं: सक्षम = "सत्य" –
अलार्म मैनेजर.cancel (पिंटेंट) का उपयोग करें; अलार्म प्रबंधक को रद्द करने के लिए। – user370305
'डिफ़ॉल्ट रूप से एंड्रॉइड: सक्षम' 'सत्य' – Tautvydas