Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Pending Intent function in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 254
    Comment on it

    If you want to create a pending intent function check my below example. Here I have created MainActivity class and MyReciver class to show get data and in this class I have used toast function. Below code will clearly described you how to create Pending Intent function.

    Step(1)-MainActivity-

    public class MainActivity extends Activity {
    
          @Override
            protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_main);
                 Button b1 = (Button) findViewById(R.id.button1);
                 b1.setOnClickListener(new View.OnClickListener() {
                     @Override
                      public void onClick(View v) {
                          // TODO Auto-generated method stub
                            startAlert(v);
                      }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
                     getMenuInflater().inflate(R.menu.main, menu);
                     return true;
        }
    
        public void startAlert(View view) {
                    EditText text = (EditText) findViewById(R.id.editText1);
                    int time = Integer.parseInt(text.getText().toString());
                    Intent intent = new Intent(this, MyReceiver.class);
                    PendingIntent pend_intent = PendingIntent.getBroadcast(
                                this.getApplicationContext(), 0, intent, 0);
            //calls the alarm
                     AlarmManager alarmManager = (AlarmManager) getSystemService(
                                            ALARM_SERVICE);
                     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                       + (time * 10000), pend_intent);
        }
    

    Step(2)-Create MyReciver class -

    public class MyReceiver extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
                      Toast.makeText(context, "Times Up!!!!", Toast.LENGTH_LONG).show();
        }
    
    }
    

    Step(3)-Activity_Main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="172dp"
            android:text="Click me" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/button1"
            android:layout_marginTop="63dp"
            android:ems="10" />
    
    </RelativeLayout>
    

    Step(4)-AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.alert_broadcast_receiver"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.alert_broadcast_receiver.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <receiver android:name="MyReceiver" >
            </receiver>
        </application>
    
    </manifest>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: