Created
November 5, 2016 16:08
-
-
Save Nouman16/e7a191e2a52fd340e22240ce1314b2ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
Button buttonStart; | |
Button buttonStop; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
buttonStart = (Button)findViewById(R.id.startService); | |
buttonStop = (Button)findViewById(R.id.stopService); | |
startMyService(); | |
stopMyService(); | |
} | |
public void startMyService(){ | |
buttonStart.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Construct an intent that will execute the AlarmReceiver | |
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class); | |
// Create a PendingIntent to be triggered when the alarm goes off | |
final PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), MyAlarmReceiver.REQUEST_CODE, | |
intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
// Setup periodic alarm every 5 seconds | |
long firstMillis = System.currentTimeMillis(); // alarm is set right away | |
AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); | |
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP | |
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY | |
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, | |
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent); | |
} | |
}); | |
} | |
public void stopMyService(){ | |
buttonStop.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(getApplicationContext(),MyServiceClass.class); | |
stopService(intent); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
// AlarmReciever Class | |
public class MyAlarmReceiver extends BroadcastReceiver { | |
public static final int REQUEST_CODE = 12345; | |
// Triggered by the Alarm periodically (starts the service to run task) | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Intent i = new Intent(context, MyServiceClass.class); | |
context.startService(i); | |
} | |
} | |
//Service Class | |
public class MyServiceClass extends IntentService { | |
public MyServiceClass() { | |
super("My_Worker_Thread"); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Toast.makeText(getApplicationContext(),"Service Start",Toast.LENGTH_LONG).show(); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); | |
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext()); | |
stackBuilder.addParentStack(MainActivity.class); | |
stackBuilder.addNextIntent(notificationIntent); | |
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); | |
Notification notification = builder.setContentTitle("Weather App Notification") | |
.setContentText("Don't miss New Weather update!") | |
.setTicker("New Weather Alert!") | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentIntent(pendingIntent).build(); | |
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0, notification); return super.onStartCommand(intent, flags, startId); | |
} | |
@Override | |
public void onDestroy() { | |
Toast.makeText(getApplicationContext(),"Service Stopped",Toast.LENGTH_LONG).show(); | |
super.onDestroy(); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment