Stackview Widget - Items not loading - Java for Android App Development

I've tried to create simple dialer widget that will get contacts from contact book, put them into stackview and when clicked create a popup where you can call or create a message. I worked well as an application, but when I turned it into actual widget, I've encountered something weird. Despite all the methods firing, the items are not updated and stay on loading layout.
I've compared it to the example, tried to switch parts of code and nothing works. Even funnier, my code works perfectly when I put it in example widget, but example code doesn't work in mine. The same thing happens for listview and gridview. Is there any other file I should edit?
I provide the code, layouts and manifest. Logcat shows all methods firing in right order, so there's nothing to see there.
WidgetProvider.java
Code:
public class WidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds){
Log.d("WIDGET", "Called onUpdate()");
for (int i = 0; i < appWidgetIds.length; ++i){
// Set up the intent that starts the StackViewService, which will
// provide the views for this collection.
Intent intent = new Intent(context, WidgetService.class);
// Add the app widget ID to the intent extras.
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// Instantiate the RemoteViews object for the app widget layout.
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main);
rv.setRemoteAdapter(R.id.stackView, intent);
rv.setEmptyView(R.id.stackView, R.id.emptyView);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
Log.d("WIDGET", "Widget updated");
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
WidgetService.java
Code:
package com.chmis.superdialer;
import java.util.Collections;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
Context context;
Intent intent;
public StackRemoteViewsFactory(Context applicationContext, Intent intent) {
this.context = applicationContext;
this.intent = intent;
}
@Override
public void onCreate() {
Log.d("SERVICE", "Called onCreate()");
//Get contact Cursor
Cursor contactCursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
//Get contacts from cursor, store them as Contact objects inside ArrayList
while (contactCursor.moveToNext()) {
String name = contactCursor.getString(contactCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = contactCursor.getString(contactCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
long id = contactCursor.getLong(contactCursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Contact.array.add(new Contact(name, phone, id));
}
//Sort contacts by name
Collections.sort(Contact.array, new ContactComparator(Contact.JOHN_SMITH));
}
@Override
public RemoteViews getViewAt(int position) {
Log.d("SERVICE", "Called getViewAt(" + position + ")");
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main);
rv.setTextViewText(R.id.contactNameTextView, Contact.array.get(position).getDefaultName());
// Return the remote views object.
return rv;
}
@Override
public int getCount() {
Log.d("SERVICE", "Called getCount(), returned " + Contact.array.size());
return Contact.array.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void onDataSetChanged() {}
@Override
public void onDestroy() {
Contact.array.clear();
Log.d("SERVICE", "Called onDestroy()");
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<StackView
android:id="@+id/stackView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:loopViews="true" />
<TextView
android:id="@+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/transparent"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="@string/no_contacts" />
</FrameLayout>
contact_stack_item
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent_dark" >
<ImageView
android:id="@+id/contactPhotoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/contact_image_description"
android:src="@drawable/contact_photo_default" />
<TextView
android:id="@+id/contactNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/contactPhotoImageView"
android:layout_alignLeft="@+id/contactPhotoImageView"
android:layout_alignRight="@+id/contactPhotoImageView"
android:background="@color/transparent_dark"
android:gravity="center_horizontal"
android:text="@string/contact_name_placeholder"
android:textColor="@android:color/white" />
</RelativeLayout>
manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chmis.superdialer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:label="@string/app_name" >
<receiver android:name="WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider_info" />
</receiver>
<service
android:name="WidgetService"
android:exported="false"
android:permission="android.permission.BIND_REMOTEVIEWS" />
</application>
</manifest>
@edit I've copied the whole service code again and now it decided to work. I don't know anything anymore.

Related

Problem when updating a Widget by an activity

Hello all together,
i've got a little problem updating my widget.
I wanted to add a widget to an existing app (Static IP Toggle).
This app changes between DHCP and static IP configuration when started =>
app started -> DHCP
app started again -> static IP, and vice versa.
I got a request from a user of this app, that it would be nice if there would be a widget included, showing which mode is active (either DHCP or static IP), so I added the widget as follows:
Added lines, concerning the former version without widget, are highlighted in blue.
First I declared the Widget in AndroidManifest.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chemdroid.staticiptoggle"
android:installLocation="internalOnly" android:versionName="1.2" android:versionCode="3">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".StaticIPToggle"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
[COLOR="Blue"]<receiver android:name=".Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>[/COLOR]
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>
The AppWidgetProvider was declared in /xml/widget.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_dhcp">
</appwidget-provider>
I created 2 layouts, one for DHCP, one for static IP. Included was an ImageButton for showing either DHCP or static IP.
Widget.java was created, for setting the specified layout and starting the activity for changing the settings:
Code:
package com.chemdroid.staticiptoggle;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.widget.RemoteViews;
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int use_static_ip;
RemoteViews remoteViews;
try {
use_static_ip = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP);
if (use_static_ip == 0) { //DHCP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_dhcp);
} else { //static IP
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_static);
}
Intent call_activity = new Intent(context, StaticIPToggle.class);
PendingIntent pending_call_activity = PendingIntent.getActivity(context, 0, call_activity, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_icon, pending_call_activity);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
At last, the activity was changed to update the widget, after IP settings have been changed:
Code:
package com.chemdroid.staticiptoggle;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings;
import android.widget.Toast;
public class StaticIPToggle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int use_static_ip;
try { --> Code für Änderung der Config
[COLOR="Blue"]AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName componentName = new ComponentName(getApplicationContext(), Widget.class);
int[] ids = appWidgetManager.getAppWidgetIds(componentName);
Intent update_widget = new Intent();
update_widget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
update_widget.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
getApplicationContext().sendBroadcast(update_widget);[/COLOR]
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}
}
But now I have this bug:
If the widget is onClicked, it shows the energy-settings-widget for a short period (~0,5sec-1sec) before the desired widget is shown. A screenshot is attached.
First there's the widget as in the left picture, then changes to the middle picture and either stays there or changes to the right picture after ~0,5-1sec.
Unfortunately I can't see any mistake in the code, do you?
Greetz Oli
Nobody here who can help me? :-/
I really can't see any mistake in my code, but what's the problem there?
Solved the problem by sending an own intent that is received from the widget -> onReceive: call onUpdate

[Q] Need help with discount calculator

I am a newbie in android dev.
I want to develop a very small and simple discount rate calculator.
in which there will be drop down menu with options like:
calculate 10%
calculate 20%
calculate 30%
Based on the selection of option from the drop down menu the discount rate should change.
After selection, there will be a textbox which should ask user to enter price.
and on the click even of "Calculate" button, the resultant amount (discounted rate) should be displayed.
I guess it is very simple for you guys. If anyone can help me with the code, i will deeply appreciate.
thank you
ok this is what I have done till now:
I created an Android Project names as :MyDC
MyDC.java
Code:
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
setContentView(R.layout.main);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
}
}
This is main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price"
/>
<EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
<Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
</LinearLayout>
This is string.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi, MyDC!</string>
<string name="app_name">My, DC</string>
<string name="spinner1">Choose discount type</string>
</resources>
This is what I am able to get:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
I am able to calculate discount on the button click, BUT i have hardcoded the discount rate.
I want to select discount rate based on the dropdown menu (I don't know how to set dropdown values with different discount rate in this problem)
In the dropdown menu I would like to have values like this:
Type 1 discount
Type 2 discount
Type 3 discount
Here is the respective discount rate for the above drop-down values:
Type 1 discount - 10%
Type 2 discount - 15%
Type 3 discount - 18%
PLZ HELP ME
Ok somehow I was able to add spinner tool on my application.
Now the question is how to select discount rate based on selected value from spinner.
PLZ HELP ME WITH THE CODE
Here is my current code:
MyDC.java
Code:
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/planet_prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price"
/>
<EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
<Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
</LinearLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hi, MyDC!</string>
<string name="app_name">My, DC</string>
<string name="spinner1">Choose a planet</string>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
</resources>
Anyone ? Please Help ?
Im sorry to not be of very much help I studied java as a highschool unfortunately it was senior year and I am good at slacking. I would like to add suggestions though. first off i would try to find someway of implementing a way to get closer increments such as 5 or 1 %. such as a text box. let me sift through your code and see what i can see. are you having specific errors or what. we need some info.
Ok i looked at your code and im having a hard time reading it just because it isnt the norm that I look at. first off ya need to name your variables more recognizable things then x and y. that will make coding immensely more easy. also I am not seeing where you gathered the information from the drop down or spinner. at some point in the code you have to collect the information from the drop down or spinner and use it for the percent. also i didn't see any point where you gave the drop down any values. if you have any questions or anything feel free to pm I have xda app on my phone so even if not im not online ill get a notification on my phone.
{SBR}_L3GION said:
Ok i looked at your code and im having a hard time reading it just because it isnt the norm that I look at. first off ya need to name your variables more recognizable things then x and y. that will make coding immensely more easy. also I am not seeing where you gathered the information from the drop down or spinner. at some point in the code you have to collect the information from the drop down or spinner and use it for the percent. also i didn't see any point where you gave the drop down any values. if you have any questions or anything feel free to pm I have xda app on my phone so even if not im not online ill get a notification on my phone.
Click to expand...
Click to collapse
Hi,
Yes, I know I haven't defined values to drop down because that is the thing I need help with.
I also posted above this:
"Now the question is how to select discount rate based on selected value from spinner.
PLZ HELP ME WITH THE CODE"
I need help - How to define discount rate based on selection of drop down values.
Please help me with the code....
I also tried doing this quite awhile with an app I was writing and then discovered I had not enough experience to with the android system to write the program I should look it up and fix both of our programs but I've had a migraine for days and I'm just getting close to sleeping but when I get a chance I will look up giving values to the drop down and collecting them unless you want to google it first.
Sent from my Synergized xda Premium Evo
I have tried to find the solution on google but didn't get anything but somehow from google I was able to do (whatever I did above)
thanks, I think that would be great, if you can test my program on your side by that we both will be on the same page.
That's fine, you can test it whenever you get some time.
I appreciate your help!
I would setup radio buttons for the discount percentages
0 10% 0 20% etc.
Name each radio button and use a case statement to set a float to it's value:
switch(radioGroupName) {
case 10Percent:
int discountValue = .10
break;
...
Then do something like:
int discountedAmount = usersAmount - (usersAmount * discountValue);
Try this code.
myDC.java
Code:
package com.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class myDC extends Activity {
double initial_price;
double discount;
double final_price;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount=10;
}
else if (pos==1){
discount=15;
}
else if (pos==2){
discount=18;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount1 = (EditText) findViewById(R.id.amount1);
if (amount1.length()>0 ) {
initial_price=Double.parseDouble(amount1.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price);
final_price=initial_price-(initial_price*discount/100);
final_price_text.setText("Final Price : "+final_price);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount1"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price"></TextView>
</LinearLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, main!</string>
<string name="app_name">myDC</string>
<string-array name="discount">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
</resources>
Re: Need Help
Hello nemoc 23 ,
Firstly thank you very much for replying back with a solution.
It did work and solved my problem upto some extend.
Now what I only want 2 things to be done:
1) Right now, if we enter price in decimal or anything, if the final result is in decimal, it is being displayed as EXACT something like 99.9999999
How to limit the RESULT upto 2 decimal points only ?
2) What I am trying to do is to have this calculator on 3 different pages (in each page I will just modify the discounted rate). So I want a homepage (which will work as MENU) with 3 or 4 buttons and each button should be connected with specific page that will have ITS OWN above discount calculator.
Could you please show me how to create a homepage screen (which will work as MENU) and 3 or 4 buttons on it which are linked with different pages (in each page you can copy the same above calculator and I will modify the rate by myself)
thank you
Why 3 pages? Just do this:
Enter discount amount: ___________
Enter discount rate:______
Discounted Amount: ___________
Easy peasy.
Rootstonian said:
Why 3 pages? Just do this:
Enter discount amount: ___________
Enter discount rate:______
Discounted Amount: ___________
Easy peasy.
Click to expand...
Click to collapse
I know it is easy but it is not what I am looking for. My requirement is what I posted above. Thanks
Help
Hello nemoc 23 ,
Just giving you an update. I did try the tutorial you asked me to try. I am having some problem in it which you can see here: http://forum.xda-developers.com/showthread.php?p=15612542
Coming back my question (this calculator topic) - I created a screen layout to give you an idea what I want to do and need your help with.
This is what I want to do:
In this each button click will open separate page (or I guess it is called activity). I have mentioned on button what each button will open.
Please help me. thanks
Intent i = new Intent(this, calculator1.class);
startActivity(i);
You are still better off creating just ONE new Activity and passing to it which calculator you want to run. It is poor programming to code 3 activities when one will suffice. You'll need "extras" to do this.
Need Help
Ok somehow I was able to code for this problem but I am not getting desired output.
I have 3 buttons on my welcome screen "calculator", "calculator2", "exit" - My buttons are not working, I don't know why.
**Below is my complete code / programs:**
**welcome.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class welcome extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome1);
//////// MENU //////////
Button playBtn = (Button) findViewById(R.id.playBtn);
playBtn.setOnClickListener(this);
Button playBtn2 = (Button) findViewById(R.id.playBtn);
playBtn2.setOnClickListener(this);
Button exitBtn = (Button) findViewById(R.id.exitBtn);
exitBtn.setOnClickListener(this);
}
/**
* Listener for game menu
*/
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.playBtn :
i = new Intent(this, testcalculator.class);
startActivity(i);
break;
case R.id.playBtn2 :
i = new Intent(this, testcalculator2.class);
startActivity(i);
break;
case R.id.exitBtn :
finish();
break;
}
}
}
**welcome1.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id ="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
/>
<Button
android:id ="@+id/playBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator2"
/>
<Button
android:id ="@+id/exitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
/>
</LinearLayout>
**testcalculator.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class testcalculator extends Activity {
double initial_price;
double discount;
double final_price;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount=10;
}
else if (pos==1){
discount=15;
}
else if (pos==2){
discount=18;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount1 = (EditText) findViewById(R.id.amount1);
if (amount1.length()>0 ) {
initial_price=Double.parseDouble(amount1.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price);
final_price=initial_price-(initial_price*discount/100);
final_price_text.setText("Final Price : "+final_price);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
**testcalculator.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount1"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price"></TextView>
</LinearLayout>
**testcalculator2.java**
Code:
package com.testcalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
import android.widget.Toast;
public class testcalculator2 extends Activity {
double initial_price;
double discount2;
double final_price2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.discount2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0){
discount2=20;
}
else if (pos==1){
discount2=25;
}
else if (pos==2){
discount2=28;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Button calculate2 = (Button) findViewById(R.id.calculate2);
calculate2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText amount2 = (EditText) findViewById(R.id.amount2);
if (amount2.length()>0 ) {
initial_price=Double.parseDouble(amount2.getText().toString());
TextView final_price_text = (TextView) findViewById(R.id.final_price2);
final_price2=initial_price-(initial_price*discount2/100);
final_price_text.setText("Final Price : "+final_price2);
}
else {
Toast.makeText(getBaseContext(), "Please enter price", Toast.LENGTH_SHORT).show();
}
}
});
}
}
**testcalculator2.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner2"></Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter the price" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/amount2"
android:text=""
android:layout_width="match_parent"
android:numeric="decimal"></EditText>
<Button
android:text="Calculate Result"
android:id="@+id/calculate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:id="@+id/final_price2"></TextView>
</LinearLayout>
**AndroidManifest.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testcalculator"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".welcome"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".testcalculator" />
<activity android:name=".testcalculator2" />
</application>
</manifest>
**strings.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, testcalculator!</string>
<string name="app_name">testcalculator</string>
<string-array name="discount">
<item>10% discount A</item>
<item>15% discount B</item>
<item>18% discount C</item>
</string-array>
<string-array name="discount2">
<item>20% discount A</item>
<item>25% discount B</item>
<item>28% discount C</item>
</string-array>
</resources>
**main.xml**
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
When I click Button1 "Calculator" - I get error saying ""The application testcalculator (process com.testcalculator) has stopped unexpectedly. Please try again.""
Button2 "Calculator2" doesn't do anything and same with 3rd button "exit:
**Please help me!!!**
You got a couple options. First instead of the startActivity() throw in a Toast so you can see if your onClick() is ok. You can also throw some inline clicklisteners (anonymous inner class) inside the setOnClickListener() instead of using 'this'. I find that way is a bit easier to organize.
Annd might I suggest you follow the Java guidelines for capitalisation of class names. Its confusing when reading your code and seeing testcalculator.class when usually instances of objects are lower case while the class name is capitalized. Like TestCalculator.class
From something awesome

[Q] Choose a string in spinner

Hey guys!
I am working on my first app, cut me some slack for asking please .
So I'm stuck on a little problem:
I have a reader activity and I put a spinner on top of the screen to choose the string where the text comes from:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner
android:id="@+id/kapitelspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/kapitel_prompt" />
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/kapitel1"
/>
</ScrollView>
</LinearLayout>
The TextView in the ScrollView is supposed to be the selected text from the spinner.
Code:
package com.asm.reader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class BookActivity extends Activity {
static final private int CHOOSE_KAPITEL = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
}
Code:
package com.asm.reader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I'm trying to make the spinner choose the string of the textview but i have no idea how to do it. (Already did the toast part)
If anyone could help me it would be greatly appreciated!
Thanks !
bump. the last problem im stuck on
The way I understand what you're trying to do is select an item from the spinner, and set the TextView according to the selected spinner item.
If that is the case you will, for starters, need to set an id attribute for the TextView in the layout xml file.
Then within your onItemSelected() method you need to create a reference to the TextView the same way you did it with the spinner; using the findViewById() method.
Then using that reference you can set the text of the TextView using the same string you got when you created the Toast notification.
Hope that helps!

How to set the background of an activity

Ok so im just practising my android skills. What I did was basically make an app that lets the user choose what image to set as the wallpaper or the background of the app.
Ive set the wallpaper up but couldnt find a way to setting the background up for the app/activity.
XML:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/IVDisplay"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/bentley" />
<HorizontalScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="100dp" >
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img1"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
android:src="@drawable/bentley" />
<ImageView
android:id="@+id/img2"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
android:src="@drawable/bugatti" />
<ImageView
android:id="@+id/img3"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
android:src="@drawable/dodge" />
<ImageView
android:id="@+id/img4"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/img5"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
android:src="@drawable/rollsroyce" />
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="@+id/wallpaperbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set as wallpaper" />
<Button
android:id="@+id/bkgbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set as Background" />
</LinearLayout>
Java:
Code:
package com.examples.hello;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class PAGEONE extends Activity implements OnClickListener {
ImageView display;
int tophone;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bkgd);
display = (ImageView) findViewById(R.id.IVDisplay);
tophone = R.drawable.bentley;
Button wallpap = (Button) findViewById(R.id.wallpaperbutton);
Button bkg = (Button) findViewById(R.id.bkgbutton);
ImageView img1 = (ImageView) findViewById(R.id.img1);
ImageView img2 = (ImageView) findViewById(R.id.img2);
ImageView img3 = (ImageView) findViewById(R.id.img3);
ImageView img4 = (ImageView) findViewById(R.id.img4);
ImageView img5 = (ImageView) findViewById(R.id.img5);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
img3.setOnClickListener(this);
img4.setOnClickListener(this);
img5.setOnClickListener(this);
wallpap.setOnClickListener(this);
}
[user=439709]@override[/user]
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.img1:
display.setImageResource(R.drawable.bentley);
tophone = R.drawable.bentley;
break;
case R.id.img2:
display.setImageResource(R.drawable.bugatti);
tophone = R.drawable.bugatti;
break;
case R.id.img3:
display.setImageResource(R.drawable.dodge);
tophone = R.drawable.dodge;
break;
case R.id.img4:
display.setImageResource(R.drawable.ic_launcher);
tophone = R.drawable.ic_launcher;
break;
case R.id.img5:
display.setImageResource(R.drawable.rollsroyce);
tophone = R.drawable.rollsroyce;
break;
case R.id.wallpaperbutton:
InputStream yea = getResources().openRawResource(tophone);
Bitmap hmm = BitmapFactory.decodeStream(yea);
WallpaperManager mywallpaper = WallpaperManager.getInstance(getApplicationContext ());
try{
mywallpaper.setResource(tophone);
}catch(IOException e){
e.printStackTrace();
}
}
}
}
I dont know what methods to use. I was thinking of setting up individual pictures as the layouts (so I'd have 5 extra layouts since I have 5 pictures), but that wouldn't make any sense.
You need to give your linearlayout an id and then find the view by normal ways and use:
yourLayout.setBackgroundDrawable(Drawable d);
Code:
LinearLayout linLay = (LinearLayout) findViewById(R.id.theLinearLayoutId);
//set background to a color
linLay.setBackgroundColor("#404040");
//set background to a drawable
linLay.setBackgroundDrawable(drawableItem);
//set background to a resource
linLay.setBackgroundResource(R.id.backgroundResource);
Or just
Code:
getWindow().getDecorView().setBackgroundDrawable(*yourDrawable*);
After setContentView
Thanks for your help guys. I followed Chris95x8's advice since its only one line of code and really, who can deny one line of code??
Anyways, took a few tries to make it work but finally did it. I put that statement after the set content view but got errors and plus that statement is deprecated so I used this methd:
getWindow().getDecorView().setBackgroundResource();
I just pasted it into the switch statement I created earlier:
case R.id.bkgbutton:
getWindow().getDecorView().setBackgroundResource(tophone);
break;
Whilst I was at it, created a few toasts letting the user know the background/wallpaper has been set.
One thing I dont understand, what does it mean by context? For example, when using the toast method:
Toast df = Toast.makeText(context, text, duration)
What does the context mean in the bracket/parenthesis ?
TwilightLoz said:
Thanks for your help guys. I followed Chris95x8's advice since its only one line of code and really, who can deny one line of code??
Anyways, took a few tries to make it work but finally did it. I put that statement after the set content view but got errors and plus that statement is deprecated so I used this methd:
getWindow().getDecorView().setBackgroundResource();
I just pasted it into the switch statement I created earlier:
case R.id.bkgbutton:
getWindow().getDecorView().setBackgroundResource(tophone);
break;
Whilst I was at it, created a few toasts letting the user know the background/wallpaper has been set.
One thing I dont understand, what does it mean by context? For example, when using the toast method:
Toast df = Toast.makeText(context, text, duration)
What does the context mean in the bracket/parenthesis ?
Click to expand...
Click to collapse
Hi,
Please check this out to know what the Context class does - http://stackoverflow.com/questions/3572463/what-is-context-in-android
Good luck

Start/Stop Notificationlistener

Hello,
At the moment I have a big problem, how can i stop the Notificationlistener?
Is there a good way to start and stop it at a specific time?
Or is there another possibility to add the Notificationlistener to my project?
thanks for help
Manifest:
Code:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.myapplication" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="test.myapplication.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".NLService" android:label="@string/app_name" android[emoji14]ermission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service> </application> </manifest>
NLService:
Code:
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
@Override
public void onCreate() {
super.onCreate();
Log.v("NL","START");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v("NL","STOP");
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG,"********** onNotificationPosted");
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
Main:
Code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void buttonClicked(View v){
if(v.getId() == R.id.btnCreateNotify){
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder ncomp = new NotificationCompat.Builder(this);
ncomp.setContentTitle("My Notification");
ncomp.setContentText("Notification Listener Service Example");
ncomp.setTicker("Notification Listener Service Example");
ncomp.setSmallIcon(R.drawable.ic_launcher);
ncomp.setAutoCancel(true);
nManager.notify((int)System.currentTimeMillis(),ncomp.build());
}
else if(v.getId() == R.id.btnClearNotify){
Log.v("NL", "CLEAR");
}
}
}
From a brief overview of some questions regarding this issue, it seems it stopping NotificationListenerService using stopSelf() or stopService(Intent intent) not always work as expected (you should try this though).
A simple workaround is to move the actual logic to another service, and have the NotificationListenerService start it only if the conditions for handling it are met.
how can i do this i tried it but i wasn't succesful. could you give me an example?
joko15 said:
how can i do this i tried it but i wasn't succesful. could you give me an example?
Click to expand...
Click to collapse
In your original service onStartCommand pass the intent you receive (or create one yourself) and start the workaround service with it:
Code:
startService(new Intent(this, YOUR_WORKAROUND_SERVICE_CLASS.class));
Handle the intent in the onStartCommand method of the workaround service.
will i have to change something in my manifest?
In my NLService I have to add the function onStartCommand what will i have to add in this function?
joko15 said:
will i have to change something in my manifest?
In my NLService I have to add the function onStartCommand what will i have to add in this function?
Click to expand...
Click to collapse
You'll need to add the new service to your manifest
and what will come in to onstartcommand function?
joko15 said:
and what will come in to onstartcommand function?
Click to expand...
Click to collapse
The intent you send it through startService command.
If you want to understand more about services, try this from Android Developer.

Categories

Resources