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

Issues with android blur using Renderscript
Android App crashes when trying to ramdomize string
RealmChangeListener sometimes not called
Android: How to create a navigation drawer with custom layout
Sugar orm create table issue
I'm developing two apps and for one of apps I copied a debug key-store from other machine. Will it reflect any problems for second app?
install_failed_older_sdk error Delphi 10.1 Berlin
GPS effect on android accelerometer data
Python android app crashes after PIL.Image.open()
requestLayout() improperly called by android.widget.ListView during second layout pass exception
How can i implement this without using showcase library or any other third party library
how to set camera white balance using rgb color using with seekbar
Unused Screen Overlay Detected permission Dialog
Check the SMS Order in Android
How can I use FirebaseRecyclerAdapter for two Item layouts in a Chat Application
GitHub Api v3 failed to authorize

Categories

HOME
url-redirection
django
hyperlink
deep-learning
developer-tools
polymer
alexa-skills-kit
case
static
pascal
serialization
jwplayer
sh
requirements
error-handling
appcelerator
static-site
telegram-bot
limit
interpolation
flume
elasticsearch-5
bing-maps
owin
commonjs
line
dynatrace
gource
mongodb-csharp
microsoft-dynamics-nav
soci
sse
systemtime
php-mysqlidb
scriptella
roundup
xunit
tiki-wiki
stackexchange
ms-access-web-app
linked-data
genexus-sd
tizen-tv
alm
database-comparison
gojs
fossil
connection-pool
password-protection
plaintext
placeholder
berkeley-db-je
httr
plan-9
pom.xml
datamaps
manova
bbpress
mouseleave
cvxpy
node-request
jquery-jscrollpane
tooleap
omniauth-facebook
project-organization
lz77
kendo-upload
mobiledoc-kit
dynatree
file-move
pypiserver
include-guards
toolkit
liquid-layout
mraid
apache-spark-1.3
android-vibration
machine-instruction
codeplex
rcaller
cancellation
jjaql
vbe
ecslidingviewcontroller
gwt-openlayers
model-driven-development
joomla-template
video-codecs
http-status-code-410
resource-management
monotouch.dialog
jquery-blockui
vfs
predicatebuilder
koken
flex-mobile
workflow-services
fsevents
mysql-error-1205
diagrams
binomial-heap
sifr
cldc
fail-fast-fail-early
business-model

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