android


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

Related Links

How to improve memory usage with BitmapFactory.decodebyteArray()?
Send touch screen event to foreground activity (only from APK)
How to auto-pair to another Android device over Bluetooth without prompting for a pin?
I want to download an audio from the server
Can we add our application setting menu to the android device settings menu
Simulate Multi Touch in Android Emulator
Clossing expanded child views in ExpandableListActivity
Is this code Correct ? Send Email Via Android
java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.GDApplication
Can I catch Exception in C in android NDK?
how to use Service
Android VideoView repetition
How to read more than one column in database by cursor and fill into a list?
Is there a way to set up an initial orientation for the Android device
How to draw touch-enabled rect in Android
changing font style dynamically

Categories

HOME
cygwin
spark-streaming
forms
object
twig
oracle-apex-5
bitbucket-api
regular-language
missing-data
banner
odoo-8
special-characters
fabric.io
owin
watson-conversation
startup
dynatrace
plink
eip
android-intent
internationalization
dynamic-sql
asp.net-mvc-viewmodel
mapstruct
apache2.4
postscript
errbot
winpe
constexpr
recurring-events
database-comparison
memory-address
erb
rselenium
linkedhashmap
sharding
arduino-ide
getlasterror
raft
apic
flatmap
odroid
plaintext
android-softkeyboard
glm-math
bindingnavigator
outputcache
wc
flask-login
snapchat
fclose
podscms
sundials
dt
check-mk
branch-prediction
sandcastle
flex-monkey
reporting-services-2012
model-associations
tooleap
delta
refinerycms
hadoop-partitioning
upx
nio2
false-sharing
rasterize
jnlua
directdraw
vhosts
roadkill-wiki
coda
uncompress
vacuum
fail2ban
umlgraph
grouping-sets
infosphere-spl
build-script
cuba
verold
syndicationfeed
ora-06550
scriptaculous
bin
gd-graph
ice-cube
jquery-1.9
pagedown
server-administration
argouml
sqlclr
isa-swizzling
promotion-code
svk
cac
globals
nstokenfield
software-lifecycle
wspbuilder

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App