【问题标题】:Android notification安卓通知
【发布时间】:2016-11-10 21:59:36
【问题描述】:

您好,我正在尝试构建一个 android 应用程序,当应用程序在后台运行时我必须显示通知。但是当应用程序在后台运行时,我有一个问题通知没有显示。请帮助

这是我的活动代码

public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(TestActivity.this, NotifyService.class);
    TestActivity.this.startService(intent);
}

这里是服务的代码

  public class NotifyService extends Service {

final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
boolean running = false;
NotifyServiceReceiver notifyServiceReceiver;

private static final int MY_NOTIFICATION_ID=1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";

  @Override
public void onCreate() {

    notifyServiceReceiver = new NotifyServiceReceiver();
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ACTION);
    registerReceiver(notifyServiceReceiver, intentFilter);

// Send Notification
    notificationManager =
            (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    myNotification = new Notification(R.drawable.ic_launcher,
            "Notification!",
            System.currentTimeMillis()+10000);
    Context context = getApplicationContext();
    String notificationTitle = "Exercise of Notification!";
    String notificationText = "http://android-er.blogspot.com/";
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, myIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    myNotification.defaults |= Notification.DEFAULT_SOUND;
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
    myNotification.setLatestEventInfo(context,
            notificationTitle,
            notificationText,
            pendingIntent);
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {

    this.unregisterReceiver(notifyServiceReceiver);
    super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {

    return null;
}


public class NotifyServiceReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        int rqs = arg1.getIntExtra("RQS", 0);
        if (rqs == RQS_STOP_SERVICE){
            stopSelf();
        }
    }
  }
}                                                 

这是我的接收者的代码

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
        Intent myIntent = new Intent(context, NotifyService.class);
        context.startService(myIntent);
    }

最后这是我的清单

<service android:name=".Test.NotifyService"
        android:enabled="true"
        android:exported="true"/>
    <receiver android:name=".Test.AutoStartNotifyReceiver"
        android:enabled="true"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

【问题讨论】:

  • 只使用意图服务而不是服务
  • 我刚刚做了,但很抱歉我没有得到任何结果
  • 我使用了一个意图服务,我在 onHandleIntent 方法中添加了我的通知代码
  • 默认intent服务没有绑定,也运行在不同的线程中
  • 老实说,我这样做有问题,因为我是 android 的初学者,你能告诉我怎么做吗谢谢@Bhargav

标签: android notifications


【解决方案1】:

现在您正在使用未绑定服务(未绑定服务无法识别该应用程序是否在后台运行)。

所以我给你的建议是你应该使用绑定服务(bindService),bindservie 可以识别应用程序何时关闭或重新打开。

在使用绑定服务时,在 unBind() 方法中添加您的通知代码(这意味着您正在通知应用程序何时关闭并在后台运行)。

您将在此链接http://developer.android.com/guide/components/bound-services.html找到绑定服务示例和其他详细信息

【讨论】:

  • 您的意思是我应该将通知代码添加到 onBind() 方法中?
  • 首先,通过在主Activity文件中调用bindService()方法启动服务,然后在onUnbind()方法中添加通知代码,这意味着当应用程序隐藏或关闭时会出现通知。
  • 我按照您的指示进行操作,但问题仍然存在 :(
【解决方案2】:

Notification(int icon, CharSequence tickerText, long when)Notification.setLatestEventInfo(...)' are both deprecated APIs. Furthermore,setLatestEventInfo` 已从 API 中移除。

考虑使用Notification.Builder 类来创建Notification 对象:http://developer.android.com/guide/topics/ui/notifiers/notifications.html

【讨论】:

    【解决方案3】:

    Android 中没有直接的方法可以知道应用程序是否在后台。我发现了这个技巧:

    服务并不是真正需要的。扩展一个应用程序类:

    public class Application extends android.app.Application {
        private static boolean activityVisible = true;
        public static boolean isActivityVisible() {
            return activityVisible;
        }
    
        public static void activityResumed() {
            activityVisible = true;
        }
    
        public static void activityPaused() {
            activityVisible = false;
        } 
    }
    

    然后,在您的活动中,将代码放入您的 onPause()onResume() 方法中:

    final int mNotificationId = 1234567;
    NotificationManager mNotifyMgr;
    
    @Override
    public void onPause() {
        super.onPause();
        Application.activityPaused();
        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), mNotificationId, resultIntent, 0);
        mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(getResources().getString(R.string.app_name))
                .setContentIntent(resultPendingIntent)
                .setContentText("Your Text");
        mNotifyMgr.notify(mNotificationId, mBuilder.build());
    }
    
    @Override
    public void onResume() {
        super.onResume();
        Application.activityResumed();
        mNotifyMgr.cancel(mNotificationId); //we don't need a notification if the app is visible.
    }
    

    在清单中,您需要指定您正在使用 Application 类:

    <application
            android:name=".Application"
            ...>
    

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2017-12-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多