Vlad Pintea Vlad Pintea - 6 months ago 25
Android Question

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.

Answer

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>