Vlad Pintea
Vlad Pintea

Reputation: 71

Why won't the BroadcastReceiver work?

I have an activity and a service. In the service, I have a BroadcastReceiver, where I want to output a message. Why won't it show the message, when I start said service?

Activity:

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
}

}

Service:

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

UPDATE: the message will actually come up when you turn on the screen, because of the ACTION_SCREEN_ON.

Upvotes: 2

Views: 59

Answers (2)

Vito Camarda
Vito Camarda

Reputation: 41

Your code works fine, did you declare your service in the 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>

Upvotes: 1

Sagar Gangawane
Sagar Gangawane

Reputation: 1985

First of all you have to register your Broadcast in Android Manifest File. See the below code.`public class MyBroadcastReceiver extends 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);
}

}

public class MainActivity extends 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 Manifest File.

<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>

Upvotes: 0

Related Questions