Reputation: 59
I programming with android studio and I want to have a Broadcast receiver to do something every time the time of clock change (it means every single minute that for example the time of phone changed from 3:13
to 3:14
) even if the program isn't run. so I should use a Broadcast receiver with Time_Tick
Action. But It just work when the Activity created. any Idea?
MainActivity:
public class MainActivity extends ActionBarActivity {
BroadcastReceiver br;
final SmsManager sms = SmsManager.getDefault();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter i=new IntentFilter();
i.addAction("android.intent.action.TIME_TICK");
registerReceiver(new MyReceiver(),i);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyReceiver:
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
Log.i("Saaalam","saaaalaalam");
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etsiamak.broadcastrecierver" >
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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"
android:enabled="true"
android:exported="true" >
<intent-filter> <action android:name="android.intent.action.TIME_TICK"></action> </intent-filter>
</receiver>
</application>
</manifest>
Upvotes: 1
Views: 2383
Reputation: 1758
Here is the sample code for time tick. The TIME_TICK intent gets fired minute change.
static TimeReceiver tickReceiver = new TimeReceiver();
void setTimeTick()
{
registerReceiver(tickReceiver, new IntentFilter(
"android.intent.action.TIME_TICK"));
Toast.makeText(this, "Registered broadcast receiver", Toast.LENGTH_SHORT)
.show();
//Register the broadcast receiver to receive TIME_TICK
registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}
The Reciever Class
class TimeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(context, "Time tick", Toast.LENGTH_LONG).show();
}
}
Upvotes: 1
Reputation: 4533
What I understand is that you need to execute certain task after certain amount of time say one minutes. For this you can use AlarmManager.
AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE)
Example for AlarmManager: https://androidresearch.wordpress.com/2012/07/02/scheduling-an-application-to-start-later/
Or Android introduced the JobScheduler API in Android 5.0 (API 21). It is better then AlarmManager as it does not consider if the device is connected to a power plug, idle or connected.
Example for JobScheduler: how-to-use-androids-job-scheduler
Upvotes: 0
Reputation: 305
Can you add this code into Manifest
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Upvotes: 0