android广播接收器不起作用

时间:2015-09-01 13:36:50

标签: java android android-studio broadcastreceiver

我使用android studio编程,我希望每次时钟更改时都有一个广播接收器做某事(这意味着每一分钟,例如手机的时间从3:13变为{{1即使程序没有运行,也是如此。所以我应该使用带3:14动作的广播接收器。但它只是在Activity创建时才有效。任何想法?

MainActivity:

Time_Tick

MyReceiver:

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);
    }
}

清单:

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");
    }
}

3 个答案:

答案 0 :(得分:1)

以下是时间刻度的示例代码。 TIME_TICK意图被激活微小的变化。

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));    
}

接收者类

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();
    }
}

答案 1 :(得分:0)

您可以将此代码添加到Manifest

<intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

答案 2 :(得分:0)

据我所知,你需要在一定时间后说一分钟后执行某项任务。为此,您可以使用AlarmManager

AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE)

AlarmManager示例:https://androidresearch.wordpress.com/2012/07/02/scheduling-an-application-to-start-later/

或Android在Android 5.0中引入了JobScheduler API(API 21)。它比AlarmManager更好,因为它不考虑设备是否连接到电源插头,空闲或连接。

JobScheduler示例:how-to-use-androids-job-scheduler