daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Alarm extends BroadcastReceiver
  2.     {    
  3.          @Override
  4.          public void onReceive(Context context, Intent intent)
  5.          {  
  6.              Toast.makeText(context, "Starting Alarm Manager", Toast.LENGTH_LONG).show(); // For example
  7.  
  8.              PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  9.              PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
  10.              wl.acquire();
  11.  
  12.              // Put here YOUR code.
  13.              Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
  14.  
  15.              wl.release();
  16.          }
  17.  
  18.      public void SetAlarm(Context context)
  19.      {
  20.          Toast.makeText(context, "Setting Alarm Manager", Toast.LENGTH_LONG).show(); // For example
  21.  
  22.          AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  23.          Intent i = new Intent(context, Alarm.class);
  24.          PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
  25.          am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 , pi); // Millisec * Second * Minute
  26.      }
  27.  
  28.      public void CancelAlarm(Context context)
  29.      {
  30.          Intent intent = new Intent(context, Alarm.class);
  31.          PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
  32.          AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  33.          alarmManager.cancel(sender);
  34.      }
  35.    
  36. public class YourService extends Service
  37. {
  38.     Alarm alarm = new Alarm();
  39.     public void onCreate()
  40.     {
  41.         super.onCreate();    
  42.         Toast.makeText(YourService.this, "Service Created", Toast.LENGTH_LONG).show(); // For example
  43.  
  44.     }
  45.  
  46.     public void onStart(Context context,Intent intent, int startId)
  47.     {
  48.         Toast.makeText(YourService.this, "Setting from Service", Toast.LENGTH_LONG).show(); // For example
  49.  
  50.         alarm.SetAlarm(context);
  51.     }
  52.  
  53.     @Override
  54.     public IBinder onBind(Intent intent)
  55.     {
  56.         return null;
  57.     }
  58. }
  59.    
  60. public void onCreate(Bundle savedInstanceState) {
  61.     super.onCreate(savedInstanceState);
  62.     setContentView(R.layout.main);
  63.  
  64.     buttonStart = (Button) findViewById(R.id.buttonStart);
  65.     buttonStop = (Button) findViewById(R.id.buttonStop);
  66.  
  67.     buttonStart.setOnClickListener(this);
  68.     buttonStop.setOnClickListener(this);
  69.   }
  70.  
  71.   public void onClick(View src) {
  72.     switch (src.getId()) {
  73.     case R.id.buttonStart:
  74.         Toast.makeText(ServicesDemo.this, "Button Pressed", Toast.LENGTH_LONG).show(); // For example
  75.       Log.d(TAG, "onClick: starting srvice");
  76.       startService(new Intent(this, YourService.class));
  77.       break;
  78.     case R.id.buttonStop:
  79.       Log.d(TAG, "onClick: stopping srvice");
  80.       stopService(new Intent(this, YourService.class));
  81.       break;
  82.     }
  83.   }
  84.    
  85. <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
  86.  
  87.     <activity android:name=".ServicesDemo" android:label="@string/app_name">
  88.       <intent-filter>
  89.         <action android:name="android.intent.action.MAIN" />
  90.         <category android:name="android.intent.category.LAUNCHER" />
  91.       </intent-filter>
  92.     </activity>
  93.     <service android:enabled="true" android:name=".YourService" />
  94. <receiver  android:process=":remote" android:name="Alarm"></receiver>
  95.  
  96.   </application>
  97.   <uses-sdk android:minSdkVersion="8" />
  98.       <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
  99.  
  100. </manifest>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top