Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:06:11+00:00 2026-06-10T16:06:11+00:00

I have been working on a widget for Android. For this I am trying

  • 0

I have been working on a widget for Android. For this I am trying to display the amount of unread messages as well as the current time. The current time was working just fine, but now that I have added the messages part it crashes when I try to load it. This is my code:

public class MyTime extends TimerTask {

RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
Context context;

java.text.DateFormat format = SimpleDateFormat.getTimeInstance(
        SimpleDateFormat.SHORT, Locale.getDefault());

public MyTime(Context context, AppWidgetManager appWidgetManager) {
    this.appWidgetManager = appWidgetManager;
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    thisWidget = new ComponentName(context, LeafClockWidget.class);

}

@Override
public void run() {
    SimpleDateFormat sdf1 = new SimpleDateFormat("EEEE, MMMM dd");
    Date d = new Date(System.currentTimeMillis());
    String time1 = sdf1.format(d);

    final Uri SMS_INBOX = Uri.parse("content://sms/inbox");

    Cursor c = context.getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
    int unreadMessagesCount = c.getCount();
    c.deactivate();

    remoteViews.setTextViewText(R.id.widget_textview3, "You have " + c + " unread SMS messages.");

    remoteViews.setTextViewText(R.id.widget_textview1, time1);
    remoteViews.setTextViewText(R.id.widget_textview2, "The time is "
            + format.format(new Date(System.currentTimeMillis())));

    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}

}

I am having trouble finding where the error is in my code, so I really hope you guys can help me out!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:06:13+00:00Added an answer on June 10, 2026 at 4:06 pm

    Edit:

    First, remove c.deactivate() since it is deprecated.

    I am unfamiliar with ContentResolver‘s, so just note that any advice I give is based on my own research. I think your issue might be your WHERE clause in your Cursor query()? Check out this answer to see if it helps any. You’ll want something very similar to his/her if(c != null) and c.moveToFirst()


    LogCat is your best friend, whenever you have an issue / exception / error you should paste your LogCat output in the question so we can better help.

    Quick tip, to find errors in your code you can use try – catch and watch your LogCat

    Here is an example:

    @Override
    public void run() {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("EEEE, MMMM dd");
            Date d = new Date(System.currentTimeMillis());
            String time1 = sdf1.format(d);
        } catch(Exception e1) {
            Log.e("SimpleDateFormat", "FAILED: " + e1.getMessage());
        }
    
    
        final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
    
        try {
            Cursor c = context.getContentResolver().query(SMS_INBOX, null, "read = 0", null, null);
            int unreadMessagesCount = c.getCount();
            c.deactivate();
        } catch (Exception e2) {
            Log.e("Cursor", "FAILED: " + e2.getMessage());
        }
    
        remoteViews.setTextViewText(R.id.widget_textview3, "You have " + c + " unread SMS messages.");
    
        remoteViews.setTextViewText(R.id.widget_textview1, time1);
        remoteViews.setTextViewText(R.id.widget_textview2, "The time is "
                + format.format(new Date(System.currentTimeMillis())));
    
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this app for at least 3-4 months and just
I have been working on a widget for Android. One of the things it
Have been working on this question for a couple hours and have come close
I have been working on this sort of ATM (With a maximum of 50
I have been working on android for the last 6 months. I wonder why
I've been working through an ordered ManyToManyField widget, and have the front-end aspect of
I have been working through the tutorial by Alex Young on using flash messages
So I have been trying for hours to get this to work and I
I have a been working on a game for Android and debugging it on
Hello I have been working on this project for a while and am very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.