Alarm instance and remote mechanism AIDL tool android: process = & quot;: remote & quot;

Source: Internet
Author: User

Because each application runs in its own process space and can run another service process from the application UI, objects are often transferred between different processes. On the Android platform, a process generally cannot access the memory space of another process. Therefore, to perform a conversation, you need to break down the object into basic units that the operating system can understand and orderly pass through the process boundary. It is tedious to implement this data transmission process through code. Android provides the AIDL tool to handle this task.

Here, the simple implementation of this mechanism is implemented with an alarm clock instance:

The alarm settings are implemented through AlarmManager. AlarmManager provides the system alarm service, and AlarmManager executes the event through the onReceive method. The onReceive method registers the event, then, the android: process = ": remote mechanism is used.

[Java]
</Activity>
<Receiver android: name = ". AlarnReceiver" android: process = ": remote"/>
</Application>
Android: process = ": remote means that the component you configured will run in another process. The other is pendingIntent, and pendingIntent is a special Intent. The main difference is that the execution of the Intent is immediate, and the execution of the pendingIntent is not immediate. The operation executed by pendingIntent is essentially an Intent operation that involves parameter data transmission, but the purpose of using pendingIntent is that the execution of the Intent operation contained in it must meet certain conditions.

The following is the simple source code of the alarm:

[Java]
Public class MainActivity extends Activity
{
Button mButton1;
Button mButton2;

TextView mTextView;

Calendar calendar;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_main );
/* Instance mode */
Calendar = Calendar. getInstance ();

MTextView = (TextView) findViewById (R. id. text );
MButton1 = (Button) findViewById (R. id. set );
MButton2 = (Button) findViewById (R. id. cancle );

MButton1.setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v)
{
// Obtain the current time
Calendar. setTimeInMillis (System. currentTimeMillis ());
Int mHour = calendar. get (Calendar. HOUR_OF_DAY );
Int mMinute = calendar. get (Calendar. MINUTE );
New TimePickerDialog (MainActivity. this,
New TimePickerDialog. OnTimeSetListener ()
{
Public void onTimeSet (TimePicker view, int hourOfDay, int minute)
{
Calendar. setTimeInMillis (System. currentTimeMillis ());
Calendar. set (Calendar. HOUR_OF_DAY, hourOfDay );
Calendar. set (Calendar. MINUTE, minute );
Calendar. set (Calendar. SECOND, 0 );
Calendar. set (Calendar. MILLISECOND, 0 );
/* Create Intent and PendingIntent to call the target component */
Intent intent = new Intent (MainActivity. this, AlarnReceiver. class );
/* Obtain a PendingIntent object from the system for Intent broadcast to BroadcastReceiver */
PendingIntent pendingIntent = PendingIntent. getBroadcast (MainActivity. this, 0, intent, 0 );
AlarmManager am;
/* Get the instance for alarm management */
Am = (AlarmManager) getSystemService (ALARM_SERVICE );
/* Set the alarm clock */
Am. set (AlarmManager. RTC_WAKEUP, calendar. getTimeInMillis (), pendingIntent );
/* Set the cycle */
Am. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + (10*1000), (24*60*60*1000), pendingIntent );
String tmpS = "set the alarm time to" + format (hourOfDay) + ":" + format (minute );
MTextView. setText (tmpS );
}
}, MHour, mMinute, true). show ();
}
});

MButton2.setOnClickListener (new View. OnClickListener ()
{
Public void onClick (View v)
{
Intent intent = new Intent (MainActivity. this, AlarnReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (MainActivity. this, 0, intent, 0 );
AlarmManager am;
/* Get the instance for alarm management */
Am = (AlarmManager) getSystemService (ALARM_SERVICE );
/* Cancel */
Am. cancel (pendingIntent );
MTextView. setText ("the alarm is canceled! ");
}
});
}
/* Format the string (-> )*/
Private String format (int x)
{
String s = "" + x;
If (s. length () = 1)
S = "0" + s;
Return s;
}
}
Here, the simple implementation function is to arrive at the specific time we set, and the onReceive method will be notified to prompt the alarm! The premise is to open another thread!

The following is the implementation of another class:

[Java]
Public class AlarnReceiver extends BroadcastReceiver
{
 
@ Override
Public void onReceive (Context arg0, Intent arg1)
{
// TODO Auto-generated method stub
Toast. makeText (arg0, "the alarm time you set is reached", Toast. LENGTH_LONG). show ();
}
 
}
Below is:

 


When I set the score to 57:


An alarm prompt is displayed!
Author: ziyang1993

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.