GAMES PC DOWNLOAD FREE FULL

GAME

[Android] Notification and AlarmManager

 

 set repeating every 24 hours

Intent myIntent = new Intent(ThisApp.this , myService.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

       Calendar calendar = Calendar.getInstance();
           calendar.set(Calendar.HOUR_OF_DAY, 12);
       calendar.set(Calendar.MINUTE, 00);
       calendar.set(Calendar.SECOND, 00);


      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //


//what is language , but i got your idea .

ar time = new Date().getHours();
var min = new Date().getMinutes();
newdate =  time + ":" + min;
Ti.API.info(newdate);

var intent = Ti.Android.currentActivity.getIntent();
var pIntent = Titanium.Android.createPendingIntent(intent);
var notification = Titanium.Android.createNotification({
    contentIntent : pIntent,
    contentTitle : "Test",
    contentText : "Test",
    tickerText : "Test"
});

if(newdate == "10:00"){
    Ti.Android.NotificationManager.notify(1, notification);
} else {
    //Nothing..

}



A simple example of Alarm Service, using AlarmManager


Modify main.xml to have two button to start and cancel the alarm. 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<Button

android:id="@+id/startalarm"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Start"

/>

<Button

android:id="@+id/cancelalarm"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Cancel"

/>

</LinearLayout>


AndroidAlarmService.java, the main activity. 
package com.exercise.AndroidAlarmService;



import java.util.Calendar;



import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.os.SystemClock;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class AndroidAlarmService extends Activity {



private PendingIntent pendingIntent;





/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button buttonStart = (Button)findViewById(R.id.startalarm);

Button buttonCancel = (Button)findViewById(R.id.cancelalarm);



buttonStart.setOnClickListener(new Button.OnClickListener(){



@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub



Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);

pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);



AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);



Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND, 10);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);



Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();

}});



buttonCancel.setOnClickListener(new Button.OnClickListener(){



@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

alarmManager.cancel(pendingIntent);



// Tell the user about what we did.

Toast.makeText(AndroidAlarmService.this, "Cancel!", Toast.LENGTH_LONG).show();





}});



}

}


MyAlarmService.java, it will be started in 10 seconds triggered by AlarmManager 
package com.exercise.AndroidAlarmService;



import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.widget.Toast;



public class MyAlarmService extends Service {



@Override

public void onCreate() {

// TODO Auto-generated method stub

Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();

}



@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();

return null;

}



@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();

}



@Override

public void onStart(Intent intent, int startId) {

// TODO Auto-generated method stub

super.onStart(intent, startId);

Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

}



@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();

return super.onUnbind(intent);

}



}


Finally, modify AndroidManifest.xml to have our MyAlarmService listed as service. 
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.exercise.AndroidAlarmService"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".AndroidAlarmService"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name=".MyAlarmService" />

</application>

<uses-sdk android:minSdkVersion="4" />



</manifest>


download filesDownload the files
[Android] Notification and AlarmManager 4.5 5 Thanh Nguyen   set repeating every 24 hours Intent myIntent = new Intent(ThisApp.this , myService.class);             AlarmManager alarmManager = (AlarmM...


No comments:

Post a Comment

NEW GAME

Powered by Blogger.

Labels

Quotes

1. Những cô gái giống như những tên miền Internet, những tên đẹp mà ta thích đã có chủ nhân rồi!


Popular Posts