[英]android Broadcast Receiver not works
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. 我使用android studio进行编程,我希望每次时钟更改时(例如,电话时间从
3:13
更改为3:14
每一分钟),都需要广播接收器来执行某项操作,即使该程序没有运行。 so I should use a Broadcast receiver with Time_Tick
Action. 所以我应该将广播接收器与
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: 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>
Here is the sample code for time tick. 这是时间刻度的示例代码。 The TIME_TICK intent gets fired minute change.
TIME_TICK意图被更改为分钟更改。
static TimeReceiver tickReceiver = new TimeReceiver(); 静态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();
}
}
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>
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 。
AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE)
Example for AlarmManager: https://androidresearch.wordpress.com/2012/07/02/scheduling-an-application-to-start-later/ 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). 或Android在Android 5.0(API 21)中引入了JobScheduler API。 It is better then AlarmManager as it does not consider if the device is connected to a power plug, idle or connected.
最好使用AlarmManager,因为它不会考虑设备是连接到电源插头,闲置还是已连接。
Example for JobScheduler: how-to-use-androids-job-scheduler JobScheduler示例: 如何使用 Androids Job Scheduler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.