为什么不赢得BroadcastReceiver的工作?

时间:2016-05-26 11:32:34

标签: android android-activity android-service

我有活动和服务。在服务中,我有一个BroadcastReceiver,我想输出一条消息。当我开始说服务时,为什么不能显示消息呢?

的活动:

public class MainActivity extends AppCompatActivity
{
private Button button_sel; //adaugat

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkLicenta(); //adaugat

    // adaugat
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(MainActivity.this, MainService.class);
            startService(intent);
            Toast.makeText(MainActivity.this, "Service, Running", Toast.LENGTH_SHORT).show();
        }
    }); //pana aici

    // adaugat
    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(MainActivity.this, MainService.class);
            stopService(intent);
            Toast.makeText(MainActivity.this, "Service, Stopped", Toast.LENGTH_SHORT).show();
        }
    }); //pana aici
}

}

服务:

public class MainService extends Service
{
private static final String TAG = "HelloService";

private boolean isRunning  = false;

@Override
public void onCreate()
{
    registerReceiver(counter, new IntentFilter(Intent.ACTION_SCREEN_ON));
    Log.i(TAG, "Service, onCreate");
    Toast.makeText(MainService.this, "Service, Created", Toast.LENGTH_SHORT).show();
    isRunning = true;
}

private BroadcastReceiver counter = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(MainService.this, "LALALA", Toast.LENGTH_SHORT).show();
    }
};

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i(TAG, "Service, onStartCommand");
    Toast.makeText(MainService.this, "Service, Started", Toast.LENGTH_SHORT).show();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy()
{
    Log.i(TAG, "Service, onDestroy");
    Toast.makeText(MainService.this, "Service, Destroyed", Toast.LENGTH_SHORT).show();
    isRunning = false;
}

@Override
public IBinder onBind(Intent intent)
{
    Log.i(TAG, "Service, onBind");
    return null;
}
}

更新:由于ACTION_SCREEN_ON,当您打开屏幕时,实际上会显示该消息。

2 个答案:

答案 0 :(得分:1)

您的代码运行正常,您是否在AndroidManifest中声明了您的服务?

   <application
    android:allowBackup="true"
    android:icon="@drawable/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>
       <service
        android:name="com.example.test.MainService"
        android:exported="true" >
          <intent-filter>
            <action android:name="com.example.test.MainService" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>
</application>

答案 1 :(得分:0)

首先,你必须在Android Manifest File中注册你的广播。 请参阅下面的代码.public class MyBroadcastReceiver扩展BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
    Toast.makeText(context,"Your Alarm",Toast.LENGTH_SHORT).show();
    Vibrator vibrator= (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);
}

}

公共类MainActivity扩展了AppCompatActivity {

EditText editText;
Button btn1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText= (EditText) findViewById(R.id.edttext1);
    btn1= (Button) findViewById(R.id.btn1);
}

@Override
protected void onResume() {
    super.onResume();
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int i= Integer.parseInt(editText.getText().toString());
            Intent intent=new Intent(MainActivity.this,MyBroadcastReceiver.class);
            PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this.getApplicationContext(),2,intent,0);
            AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+i*5000,pendingIntent);
            Toast.makeText(MainActivity.this,"Alarm Set",Toast.LENGTH_SHORT).show();
        }
    });
}

}

  

Android清单文件。

<uses-permission android:name="android.permission.VIBRATE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<receiver
    android:name=".MyBroadcastReceiver">
    <intent-filter>
       <action android:name="com.example.admin.broadcastreceiverdemo">
       </action>
    </intent-filter>

    </receiver>
</application>