Issue
Actually I wanna run background thread after specific time like after 10 seconds to send Api to server but first I am trying below code for checking/testing. In AlarmReceiver Class onReceive run only one time rather I want to run after every 10 seconds. I am new to android please guide me how can I achieve my goal. Thanks
Here is MainActivity Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve a PendingIntent that will perform a broadcast
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
}
public void startAlarm(View view) {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000; // 10 seconds
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancelAlarm(View view) {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
}
Here is AlarmReceiver Class:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();
}
}
Here is Manifest File code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.majidali.alarammanager">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Solution
I think for small time difference like seconds , You can use Handler
.
Try this code,
Handler handler=new Handler();
Runnable mytask=new Runnable(){
public void run(){
//do something
Toast.makeText(Youractivity.this, "Running", Toast.LENGTH_SHORT).show();
handler.postDelayed(this, 10000);//10 sec
}
};
mytask.run();
Edit
Do some trick,
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//yourtask
setAlarm(context,10000);
}
public void setAlarm(Context context,long time)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pi);
}
public void cancelAlarm(Context context)
{
try {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
catch (Exception e)
{
}
}
}
And in your Activity,
new AlarmReceiver().setAlarm(this,10000);
Answered By - Jyoti JK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.